Use automatic controller routes in Laravel is a bad idea
Asked Answered
S

5

5

I'm coming From CodeIgniter to Laravel.

So, is a bad idea using automatic routes to all of controllers?

Route::controller(Controller::detect());

Should I use this instead creating routes in routes.php?

Spender answered 5/3, 2013 at 18:14 Comment(0)
R
10

Yes this is bad.

Controller::detect() is actually not present in Laravel 4 because it is a bit broken.

detect() will go through your filesystem and return controller files, but this is a bad idea because the order you define your routes matters. If you have any nested controllers you will find this breaking very easily.

detect() will also return files in a different order depending on the file system, so this leads to a lot of unpredictability.

I would argue that you should define all your routes any ways, it is a lot easier to read and debug.

Ribose answered 5/3, 2013 at 18:19 Comment(0)
S
5

One of interesting things about Laravel that CI does not have is that for certain pages, you can route directly to the view without needing a controller at all. Think about static pages like 'About Us'. CodeIgniter would need you to set up a controller + view for that, even though the controller will do barely anything. In case of Laravel, you can route directly to a view in this case.

Setting up routes manually will allow you to set these short-circuited routes.

Seriate answered 5/3, 2013 at 18:17 Comment(1)
Really interesting. Thanks!Spender
D
1

Automatic detection is a bad idea.

You can use routes or use Route::controller('mycontroller') or and array of controllers like Route::controller(array('mycontroller', mycontroller2');

Then you get the benefit, without the autodetect.

Distasteful answered 5/3, 2013 at 18:21 Comment(0)
A
1

in laravel 4 :

you can use Restful Controller like documentation http://laravel.com/docs/controllers#restful-controllers

But Route::controller() must take two parameters as minimum requirement first parameter stands for URL respond to ,and second parameter is name of controller

also

you can write third parameter into Route::controller() is an array with names of actions (name of action with HTTP verb )and routes names for this actions

ex:

Route::controller('users','UsersController',array(
            'getUsers' =>"listUsers" , 
           ));

route name for getUsers action is listUsers

Acrolein answered 11/9, 2013 at 23:3 Comment(0)
G
-1

Below is a good example to follow for CRUD and general purpose routing

type php arisan controller:make SampleController

edit routes.php and add

Route::resource('sample', 'SampleController'); 

Then type php artisan routes to show the newly created routes

Gustave answered 4/12, 2014 at 6:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.