Routes Laravel 5.3

Asked

Viewed 1,707 times

9

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

I wonder, how can I create other routes without being inside the web.php, I would like to separate so.

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

2 answers

9


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');
    

6

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

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 groups of routes to each of your files.

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

  • 3

    Yeah, it’s really better to include new groups directly in web.php. The other options are for cases where it is necessary to distinguish very well things. Of course organization is personal, but it’s good to follow the standard framework. + 1

  • Thank you so much for the help. It was missing to say that when defining the "middleware" it is necessary to add it in the.php kernel.

  • Ah yes, sorry, I put only to show that it was possible...hehe #edited

Browser other questions tagged

You are not signed in. Login or sign up in order to post.