Is there any way to define a specific Title in the Laravel routes?

1

In%% 5, I'm creating a menu dynamically by making a Laravel in the list of registered routes.

So, I can have a menu displayed every time a new route is created, however I'm only listing those that foreach the contém method in the controller.

So:

@foreach(Route::getRoutes() as $route)
 @if(Str::finish($route->getName(), '.index'))
 <li>
   {{ link_to_route($route->getName()) }}
 </li>
 @endif
@endforeach

However, in the second parameter of index , it would be interesting to pass a title to this route. So I would have links with the very descriptive name for the route, instead of displaying link_to_route complete.

Is there any way to create a title for this route in the creation of url (I'm not talking about the Rota option that is used internally)?

Is there any way to define in the controller method or route an attribute for me to use as the title of "as" ? Can I do this in Route ?

    
asked by anonymous 26.04.2016 / 22:31

1 answer

1

Yes, with a little creativity you can create "custom attributes" in Laravel . I mean by that you can create indexes in array passed to the route.

My suggestion is to do this with indices in Laravel internally.

For example, you could use the title attribute.

So, we can do it as follows:

  Route::get('/', [
    'uses' => 'HomeController@getIndex', 'title' => 'Página inicial'
  ]);

  Route::get('/usuarios', [
     'uses' => 'UsersController@getIndex', 'title' => 'Página de usuários'
  ]);

 // Observe que não defini 'title' aqui

 Route::post('/usuario/cadastrar', ['uses' => 'UserController@postStore']);

Next, we could do this in the view, to be able to list only rotas containing title:

 @foreach(Route::getRoutes() as $route)
     @if(array_key_exists('title', $action = $route->getAction()))
         {{ link_to_route($route->getName(), $action['title']) }}
     @endif
 @endforeach

Note that only routes containing the title index are displayed. That is, the others are skipped.

I think this is the best way to create your menus dynamically with Laravel .

I hope this can help many:)

    
27.04.2016 / 15:59