Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 773
Yubraj Pokharel Avatar asked Nov 25 '14 08:11

Yubraj Pokharel


People also ask

How to control the route actions in Laravel?

In Laravel, the Route actions can be controlled by any of the following two methods, either by using Route::resource method or by using Route::controller method. But both of them have their differences.

What is API Resource Controller in Laravel 5?

Laravel 5.5 added another method for dealing with routes for resource controllers. API Resource Controller acts exactly like shown above, but does not register create and edit routes. It is meant to be used for ease of mapping routes used in RESTful APIs - where you typically do not have any kind of data located in create nor edit methods.

Is it possible to skip controllers in Laravel?

Show activity on this post. 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:

What is resource routing in Laravel?

It is likely that users can create, read, update, or delete these resources. Because of this common use case, Laravel resource routing assigns the typical create, read, update, and delete ("CRUD") routes to a controller with a single line of code.


1 Answers

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.

like image 176
Kaustubh Joshi Avatar answered Sep 24 '22 08:09

Kaustubh Joshi