i am new to laravel, infact new to php framework , i watched the laracast laravel fundamentals videos and when the guy in the video was explaining the routes there was controllers like :Route::get("/","Controller@index")
but when i installed the the laravel 5 there was something like
Route::get("/",function(){
return view()
})
so what is the difference , and why do i need controllers??
Route - "catches the request and decides who will handle it."
Controller - "normally this will (1) get the request from the router (above), and then (2) call on the model to get the data, and then (3) call the view to render the output to the user."
Model - This wraps the database to make it easier to access data. In Laravel we use normally Eloquent to do this.
View - This is where you render the result from the data and the logic that the controller put together.
Controllers are part of MVC a pattern to divide your application into parts with their own responsibilities. @Jaysen gives a good idea of how it works. MVC consists of Model, View, Controller; models are responsible for data access, views are responsible for showing content and controllers do the logic.
// routes.php
Route::get('/', 'WelcomeController@welcome');
// WelcomeController.php
class WelcomeController extends Controller {
public function welcome()
{
return view('welcome');
}
}
is that in the first example, you write code in the routes.php file. You can do that for all routes, but it's not recommended because your routes.php file will become very big and you'll have a difficult time maintaining it. That example is good for testing purposes only.
It is recommended that you use Controller classes to write code for the routes, like it is shown in the second example. Your code for that route will be more maintainable and you'll know where to look for the code just by looking at the route in the routes.php file.