Routes Laravel 5.3

9

Good afternoon people, I noticed that Laravel 5.3 has a new folder "Route", with the routes. By default it uses web.php

I would like to know how I can do to create other routes without being inside the web.php, I would like to separate it like this.

web.php darshboard.php user.php ... etc

    
asked by anonymous 22.09.2016 / 20:33

2 answers

9

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('/', '[email protected]');

    /*
     * Isto será acessível via http://localhost/about
     * O arquivo da classe será: app/Http/Controllers/Homepage/Main.php
     */
    Route::get('/about', '[email protected]');
});

/*
 * 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('/', '[email protected]');

    /*
     * Isto será acessível via http://localhost/admin/user
     * O arquivo da classe será: app/Http/Controllers/Admin/Main.php
     */
    Route::get('/user', '[email protected]');
});

/*
 * 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('/', '[email protected]');
        Route::get('/about', '[email protected]');
    });
    
  • routes / admin.php

    <?php
    
    Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() {
        Route::get('/', '[email protected]');
        Route::get('/about', '[email protected]');
    });
    
  • 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');
    
22.09.2016 / 21:31
6

Friend, to use multiple files, there are some options, one of them is to edit this service provider App\Providers\RouteServiceProvider , where exstem the following methods:

protected function mapWebRoutes()
{
    Route::group([
        'middleware' => 'web',
        'namespace' => $this->namespace,
    ], function ($router) {
        require base_path('routes/web.php');
    });

    //Este aqui eu adicionei como exemplo, não está no core
    Route::group([
        'middleware' => ['web', 'auth'], //quantos middlewares forem necessários, apenas lembre de adiciona-los no kernel.php
        'namespace' => $this->namespace, //'App\Http\Controllers'
        'prefix' => 'user'
    ], function ($router) {
        require base_path('routes/usuario.php');
    });
}

protected function mapApiRoutes()
{
    Route::group([
        'middleware' => 'api',
        'namespace' => $this->namespace,
        'prefix' => 'api',
    ], function ($router) {
        require base_path('routes/api.php');
    });
}

In this case you would add new route groups for each of your files.

It is also possible to include these new groups in web.php itself.

    
22.09.2016 / 20:54