At the same time, it prevents you from creating your own pattern, however it is ideal to organize in Controllers and Views, and in web.php you can use Route::group
, for example:
/*
* todas rotas acessam a partir do namespace "App\Http\Controllers\Homepage"
* todas rotas acessam a partir do path "/"
*/
Route::group(['namespace' => 'Homepage'], function() {
/*
* Isto será acessível via http://localhost/
* O arquivo da classe será: app/Http/Controllers/Homepage/Main.php
*/
Route::get('/', 'Main@index');
/*
* Isto será acessível via http://localhost/about
* O arquivo da classe será: app/Http/Controllers/Homepage/Main.php
*/
Route::get('/about', 'Main@about');
});
/*
* todas rotas acessam a partir do namespace "App\Http\Controllers\Admin"
* todas rotas acessam a partir do path "/admin/"
*/
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() {
/*
* Isto será acessível via http://localhost/admin/
* O arquivo da classe será: app/Http/Controllers/Admin/Main.php
*/
Route::get('/', 'Main@index');
/*
* Isto será acessível via http://localhost/admin/user
* O arquivo da classe será: app/Http/Controllers/Admin/Main.php
*/
Route::get('/user', 'Main@user');
});
/*
* todas rotas acessam a partir do namespace "App\Http\Controllers\Api"
* todas rotas acessam a partir do path "/api/"
*/
Route::group(['namespace' => 'Api', 'prefix' => 'api'], function() {
/*
* Isto será acessível via:
* - http://localhost/api/photos
* - http://localhost/api/photos/create
* - http://localhost/api/photo/{id}/edit
*
* O arquivo da classe será: app/Http/Controllers/Api/PhotoController.php
*/
Route::resource('photos', 'PhotoController');
});
In this way the web.php will not be great, the routes remain in only one place and everything will be organized within the controllers.
Note that Closures
does not support route caching, since the controllers do, which can be an advantage.
Of course nothing prevents you from creating 3 separate files and using require_once
:
-
routes / homepage.php
<?php
Route::group(['namespace' => 'Homepage'], function() {
Route::get('/', 'Main@index');
Route::get('/about', 'Main@about');
});
-
routes / admin.php
<?php
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() {
Route::get('/', 'Main@index');
Route::get('/about', 'Main@user');
});
-
routes / api.php
<?php
Route::group(['namespace' => 'Api', 'prefix' => 'api'], function() {
Route::resource('photos', 'PhotoController');
});
-
And no routes / web.php:
<?php
require_once base_path('routes/homepage.php');
require_once base_path('routes/admin.php');
require_once base_path('routes/api.php');