Laravel :: Routes Vs. Controller

Solution:

In Laravel, you can totally skip controllers and do the task of performing business logic and generating the view in the routes.

E.g I have a link b2.com/getUsers so in routes.php I can write:

Route::get('/getUsers',function()
{
    $users=User::all();   //select * from users
    return View::make('allUsers')->with('users',$users);
}

So, here to serve the request b2.com/getUsers, we didn’t use controller at all and you can very well do this for handling all requests in your application, both get and post.

But then, if your application is large and have 500+ url’s with complex business logic then imagine putting everything in one routes.php. It will totally make it criminally messy and whole purpose of architecture will be defeated. Hence what we usually do is, reserve routes.php for routing only and write all business logic (along with generation of views inside controllers)

So the same example can be solved as:

To handle link: b2.com/getUsers, in routes.php

Route::get('/getUsers', array('before' => 'auth', 'uses' => 'MyController@getUsers'));

MyController has the method getUsers defined like this:

public function getUsers()
{
    $users=User::all();   //select * from users
    return View::make('allUsers')->with('users',$users);
}

I usually create a controller for related activities e.g for login/signup/logout. I create AuthController and all the links related to those activities are routed to AuthController through routes.php.