How the methods of the Route class work in laravel

1

I have this route but I can not understand most of them and I already read the documentation but I was left with the doubts:

Route::namespace('Admin')
     ->prefix('admin')
     ->name('admin.')
     ->middleware(['auth', 'admin'])
     ->group(function () 
    {

      Route::get('lista-productos','productosController@index);

    });

Somebody could explain to me in favor of how the namespaesce, name works, I would really appreciate it. The rest as the prefix I understand how it works like middleware but I do not understand how to implement it so that they can be called

    
asked by Luis Hernandez 06.12.2018 в 05:03
source

2 answers

1

The ->name('.admin') method is used as a common appointment factor for most or all of the routes you have that point to /admin

For example you have this group of routes:

Route::get('/admin', 'DemoController@index');

Route::post('/admin', 'DemoController@update');

Inside your forms you have something like this:

action="{{ route('admin') }}"

The two previous routes access /admin however if in the future you need your route to be: /admin-enterprise then you will have to update one by one to point to the new route.

On the other hand if your routes are named like this:

Route::get('/admin', 'DemoController@index')->name('admin');

Route::post('/admin', 'DemoController@update')->name('admin');

Even after you change them to:

Route::get('/admin-enterprise', 'DemoController@index')->name('admin');

Route::post('/admin-enterprise', 'DemoController@update')->name('admin');

The URL within your forms does not need to change, but you can still use admin because it was the name you assigned to them

The route in your forms would still be similar to this

action="{{ route('admin') }}"
    
answered by 06.12.2018 в 05:28
1

The namespace() method works very similar to the namespaces in PHP (and in almost any other language), in the case of groups of routes, it allows you to "obviate" a part of the driver's route, normally the part of App\Http\Controller , but in the specific case of the question, we are avoiding App\Http\Controller\Admin .

In other words, if we did not use this method, the route would have to be called as follows:

Route::get('lista-productos','Admin\productosController@index);

As for name('admin.') , it is no more than a prefix for the names given to group routes, although it is not being used in the code of the question. The route shown should have a name, even if it is the only one, this for scalability purposes:

Route::get('lista-productos','productosController@index)->name('products.list');

In this way the route will be referenced from any part of the application as follows:

route('admin.products.list')
    
answered by 07.12.2018 в 03:24