I'm trying to create a form with a corresponding controller method which adds a new record to the DB. Laravel Version is 4.1
app/views/projects.blade.php
<tr>
{{Form::open(array('action' => 'ProjectController@createProject', 'method' => 'post'))}}
<td>{{Form::text('project_number')}}</td>
<td>{{Form::text('title')}}</td>
<td>{{Form::text('client')}}</td>
<td>{{Form::text('comment')}}</td>
<td>
{{Form::file('xmlfile')}}<br />
{{Form::submit('Hinzufügen',array('class' => 'blue'))}}
</td>
{{ Form::close() }}
</tr>
app/controllers/ProjectController
<?php
class ProjectController extends BaseController {
public function listProjects(){
$projects = Project::all();
return View::make('projects',array('projects' => $projects));
}
public function createProject(){
/* handling the form data later
.
.
.
*/
return "Hello";
}
}
?>
Routes.php
// Project Routes
Route::get('/projects', array('as' => 'listProjects', 'uses' => 'ProjectController@listProjects'));
Route::get('/projects/{id}', array('as' => 'actionProject', 'uses' => 'ProjectController@actionProject'));
// Canal Routes
Route::get('/canals', array('as' => 'listCanals', 'uses' => 'CanalController@listCanals'));
Error Message
ErrorException Route [ProjectController@createProject] not defined. (View: /var/www/virtual/hwoern/laravel/app/views/projects.blade.php)
Show the existing projects with the list method in the projects view works fine. What have I overlooked?
All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider . The routes/web.php file defines routes that are for your web interface.
For route controller method we have to define only one route. In get or post method we have to define the route separately. And the resources method is used to creates multiple routes to handle a variety of Restful actions. Here the Laravel documentation about this.
Route model binding in Laravel provides a mechanism to inject a model instance into your routes.
You have received the Route [ProjectController@createProject] not defined
because you haven't created any post route for action ProjectController@createProject
yet.
You have to define the following route:
route.php
Route::post('new-project', array('uses' => 'ProjectController@createProject'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With