Yubraj Pokharel
Yubraj Pokharel

Reputation: 485

Laravel :: Routes Vs. Controller

As I am new to the laravel 4 after spending some few months in Codeigniter, I went through the lots of tutorials about laravel and one thing I want to be clear is what is the actual difference between Routes and Controller in laravel, because we can create and generate view in both controller and routes too. Will anyone explain me in brief when to use routes and Controller in laravel? Because in other Framework we need routes to specify some particular URL's within the apps and Controller were used to do some real tasks but in laravel I didnt get the main concept of Routes except the routing mechanism?

Upvotes: 6

Views: 5466

Answers (3)

NorthBridge
NorthBridge

Reputation: 674

Let's see what we have in both cases:

In CodeIgniter, a route is just pointing your request to a specific method of your controller:

$route['blog/joe'] = "blogs/users/34";

Here when you visit application.com/blog/joe, you will invoke the controller BlogsController, call the method users() and pass 34 as the first parameter. Nothing else. As they put it, a route in CI is just a one-to-one relationship between a URL string and its corresponding controller class/method.

Now, in Laravel, you have a lot of possibilities:

  • You can directly return a simple response
  • You can return a view
  • You can point the request to a specific controller and a method
  • You can write some logic in a closure and then decide what you want to do

You can add some additional functionality to them, like attaching filters, checking parameters upon a regex, give them separate names, etc., but this is the main functionality.

What's the reason for being able to do so much stuff? It gives you the power to use them in any way you need. Examples:

  • Need a small website, rendering static HTML? Use them like this:
Route::get('/', function()
{
    return View::make('greeting');
});
  • Need a bigger application using the traditional MVC pattern? Use like this:
Route::get('user/{id}', 'UserController@showProfile');
  • Need a RESTful approach? No problem. This will generate routes for all the CRUD methods:
Route::resource('photo', 'PhotoController');
  • Need something quick and dirty to handle a specific Ajax request? Keep it simple:
Route::post('foo/bar', function()
{
    return 'Hello World';
});

TL;DR: For very simple things without or with very little logic, use them instead of controllers. Otherwise, always stick to the MVC principles and route to your controllers, so that they're the ones who do the actual work.

Upvotes: 1

Kaustubh Joshi
Kaustubh Joshi

Reputation: 91

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.

Upvotes: 8

Mostafa Talebi
Mostafa Talebi

Reputation: 9173

The fact that you can get views or do a lot of things in Routes::any() is against MVC and separation of logic.

In Route::get("admin", function(){}), you indeed have a fast access to your route callback, which otherwise in a standard fashion must just be bound to controller. But Laravel allows you to do your job there in a closure (function(){}), instead of binding it to a controller. Anyway, it lets you, but you'd better avoid it. In Route::get() you only should go with your 'routing' and nothing more.

There is no reason for you to use callbacks in Route unless for testing or some trivial requests. So, better to avoid this:

Route::get("admin", function(){
    return View::make("admin_index");
});

And rather go with this:

Route::controller("admin", "AdminController");

And in your AdminController.php :

// I mean create a file named AdminController.php in controllers directory under app.
class AdminController extends Controller
{
   function getIndex()
   {
      return View::make("admin_index");
   }
}

Read more about Route::controller and restful controllers.

Some Notes:

  • Having the ability to add closures in your Routes allows you to make complex decisions on Routes and have a powerful routing system.

  • These callbacks let you add conditions to your route.

  • Having Controller separated from you Routes makes you application
    more extensible, less confusing and makes other coders more
    comfortable in future.

  • It allows you to focus better on your problem and finding solution,
    this physical separation is very important. Having View::make()
    inside your Route stirs all problems into each other and makes up a
    confusion for the coder.

Upvotes: 4

Related Questions