As you continually add more and more routes to Routes.php it gets bigger and bigger. How do you organize them?
5 Answers
I created a directory /application/routes/ and added files in there. Each file is just a set of routes. Then in routes.php I added the following code to include them all:
// Dynamically include all files in the routes directory
foreach (new DirectoryIterator(__DIR__.DS.'routes') as $file)
{
if (!$file->isDot() && !$file->isDir() && $file->getFilename() != '.gitignore')
{
require_once __DIR__.DS.'routes'.DS.$file->getFilename();
}
}

- 538
- 3
- 5
-
I think this is the most beautifull solution. But do you think the Directory Iterator will run each time a user is trying to access the website ? if yes, I imagine it could engender some performance problem ? – RPDeshaies Mar 17 '14 at 20:00
-
This is a very elegant solution. @Tareck117, it's possible that this could affect performance, however, not so much that it would need to be focused on straight away (although it is low hanging fruit). – Tim Groeneveld May 03 '14 at 15:40
-
1I agree that this could introduce a performance issue but it would be very small. Especially with SSDs. If you are worried about that, you can cache the file list. – coffe4u Jun 10 '14 at 22:19
even after adopting all the best practices as mentioned in the other answers, Ie: using resource controller, route groups etc.
You can simple 'include' the route files the old fashion way. as mentioned by Chris G in this comment.
you can create simple directory structure and include the route files in the route.php file.
../myroutes
--route-type-1.php
--route-type-2.php
In route.php file
include('myroutes/route-type-1.php');
There is nothing wrong in it. This is how routes are included in the packages.

- 1,250
- 12
- 16
-
2
-
1Sometimes I do forget that Laravel is PHP and it allows stuff like this, and people try to overcomplicate things and do it the "Laravel Way" when there has been a good old PHP way for many, many years. I include myself in this. – Alex McCabe Aug 15 '16 at 13:38
I usually use Group routes (because controllers tend to have the same type of filtering needed if they are related) to organize them or if you wish to/can have a smaller routes file you might want to register your controllers and do the extra validation checks of the URL's parameters inside the controller itself.

- 946
- 11
- 12
-
I think first suggestion doesn't shrink the file that much and the second would just eliminate the routes for good. Including other route files as someone suggested is better – Srneczek May 27 '16 at 13:53
Actually routs should stay slim. Just move your code to controllers and use the routs to register and redirect to them. The convention is to store one controller per file so your code becomes automatically organized.
Take a look at this
// application/controllers/sample.php
class Sample_Controller extends Base_Controller
{
public function action_index()
{
echo "Wellcome to the root" //www.testapp.com/sample
}
public function action_edit()
{
echo "Some editing functions here." //www.testapp.com/sample/edit
}
public function action_whatsoever()
{
echo "Put here whatever you like." //www.testapp.com/sample/whatsoever
}
}
The controller-action route can be registered like this:
//application/routs.php
Route::controller('admin::home');
Very straight forward and comfortable.
Update:
You can also register all your controllers with this line for the whole application automatically:
Route::controller(Controller::detect());
Or the controller with all actions:
Route::controller(Controller::detect('yourcontroller'));

- 12,361
- 6
- 53
- 72
-
3Right.. but the question is what to do when the Routes.php file becomes large from registering so many controllers. Also for simple routes and stuff, it's not always logical to create a controller for them and that will make the size of the routes file bigger too. Is best practice to create multiple routes files and `include` them in routes.php? – Chris G. Feb 26 '13 at 13:04
-
K, then I run in the wrong direction. I never heard of including multiple routes files into routes.php nether did I. I assume that nobody else out there would do it this way so this cant be the best practice. Could you post the example of your routes.php content? Maybe this can help to analyze your problem. – Hexodus Apr 04 '13 at 07:54
-
1I just extended my answer to show how to register all of your controllers incl. all methods with one line. – Hexodus Apr 04 '13 at 08:01
-
I believe in Laravel 4 that Controller::detect() no longer exists and you have to manually define them. – VeenarM Aug 09 '13 at 05:22
Alternatively you could just store the routes in a different files and then get those files using include:
Route::group(stuff,function(){
include __DIR__.DS.'routes1.php';
include __DIR__.DS.'routes2.php';
});
That gives a nice and clean way to sort handle when there's way too much code, also you could just mention
Route::controller(Controller::detect());
And then structure your controllers accordingly:
class Account_Controller extends Base_Controller {
public function action_login() {
//Login
}
public function action_logout() {
...
}
If you have to passin parameters to some function:
public function dosomething($input){
...
}
You can reach those functions like this:
http://yourapp/account/login
http://yourapp/account/logout
Then you could just call the function by appending the parameters to the URL,
http://yourapp.account/dosomething/somedata
If you need some methods to be protected then append them without the action_ so that they can't be accessed by the public.
Alternatively, you could switch to restful methods which is basically a way to respond to some input based on the type of query you get, for instance when you receive a "GET" request to the login page that means that the user needs to see the login screen, when you receive a "POST"request that would usually mean that the user is posting login data and accordingly you can respond which will help you cut down on the number of functions for more information on restful methods you can read this brilliant article by Ryan Tomayako at http://tomayko.com/writings/rest-to-my-wife
To use restful methods you need to mention restful to true and then append the action keyword before the function's name.
public $restful = true;
public function get_login() {
return View::make('login');
//This function is accessible using GET request to http://yourapp/login
}
public function post_login() {
$data = Input::get();
//do something using the Input data
//This function is accessible using a POST request to http://yourapp/login
}
This way you can eliminate the need for another route to process and verify the users credentials!
and if you don't want certain methods to be accessed using restful methods then just don't include the action keyword, (get,post,...) in the function name.
Combining restful methods with smart routing is the most efficient way to keep your code clean and safe.

- 375
- 2
- 7