Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use automatic controller routes in Laravel is a bad idea

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?

like image 896
Leabdalla Avatar asked Mar 05 '13 18:03

Leabdalla


People also ask

What is the notable difference of the controller and router in Laravel?

Router - is what defines how to parse request data. Controller - is what accepts parsed request and generates a response.

Why we use routes in Laravel?

Routing in Laravel allows users to route all their application demands to its appropriate controller. The most primary routes in Laravel acknowledge and accept a Uniform Asset Identifier together with a closure, given that it should be got to be a simple and expressive way of routing.

Which type of route binding are supported Laravel?

Laravel currently supports two types of route model bindings. We have: Implicit model binding. explicit model binding.

Why controllers are used in Laravel?

A Controller is that which controls the behavior of a request. It handles the requests coming from the Routes. In Laravel, a controller is in the 'app/Http/Controllers' directory. All the controllers, that are to be created, should be in this directory.


2 Answers

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.

like image 64
Deinumite Avatar answered Sep 21 '22 06:09

Deinumite


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.

like image 26
xbonez Avatar answered Sep 22 '22 06:09

xbonez