Making Controllers in Laravel
We can use php artisan make:controller SomeController
to make a controller.
But to use a controller, we have to set a route from web.php file.
So the actual purpose of controller is to make the web.php file to just set to routing
and do the logic inside the controller.
The controller has functions
that return view and that has the function name
inside the web.php file.
But routing examples are worth nothing.
Route::get('/tasks', 'TasksController@index');
Route::get('/tasks/{id}', 'TasksController@show');
and in controller,
public function show($id) {
$task = Task::find($id);
return view('tasks.show', compact('task'));
}
And one special thing is called route model binding,
and it let’s you to get the model from the routing.
But to do this, you have to use the same variable name as the passing parameter.
That is, we should use
Route::get('/tasks/{task}', 'TasksController@show');
and use
public function show(Task $task){
return view('tasks.show', compact('task'));
}