In prevents you from creating your own standard, however the ideal is to organize in the Controllers and Views that, and in the 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');
});
This way the web.php won’t get big, the routes stay in one place and everything will be organized inside the controllers.
Note that Closures
does not support route caching, 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 in 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');
Excellent, thank you very much!
– Rodrigo R. de Oliveira