Dynamic routes for Laravel pages

Asked

Viewed 557 times

0

I want to create a dynamic routing system, without having to code gambiarras.

Come on.

I have the Routes:

Route::group([
    'middleware' => ['web'],
    'namespace' => 'LaraShop\Front\Http\Controllers',
    'as' => 'page'
], function()
{
    /*
     * Páginas padrão
     */
    Route::get('/', 'HomeController@index')->name('.home');
    Route::get('/contato', 'PagesController@show')->name('.contact');

    /*
     * Rotas Dinâmicas
     */
    Route::get('{page}', 'PagesController@getPage');

});

Route::group([
    'middleware' => ['web', 'auth'],
    'namespace' => 'LaraShop\Admin\Http\Controllers',
    'as' => 'admin'
], function() {
    // rotas do admin
});

The point is, what’s the best way to {page} go through the /admin route without causing an error?

  • 1

    It is not correct this route because it will clash with /, with /contato etc ... cannot have something dynamic at the root if there are other routes that have the same nomenclature.

1 answer

1

Position your route at the end of the file.

Route::get('{page}', 'PagesController@getPage');

In the PageController@getPage, check which page is and if it has a view for example and if you do not find a abort(404)

Browser other questions tagged

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