Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

moe1047's avatar

what are the controllers for?

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??

0 likes
3 replies
jaysen's avatar

Controllers handle the logic part of the program.

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.

That's a very basic overview.

3 likes
luceos's avatar

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.

If you're new to php, frameworks and what to read more about these patterns, please read: http://designpatternsphp.readthedocs.org/en/latest/README.html

A very good resource in my opinion.

1 like
davorminchorov's avatar
Level 53

The difference between:

Route::get('/', function() {
    return view('welcome');
});

and

// 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.

Please or to participate in this conversation.