1

I have a prefix called admin. My all controller in admin folder.

In route I have created route like below with namespace

Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function()
{
    Route::resource('/tags', 'TagsController');

});

Now I am trying to create link in my admin panel sidebar

<a href="{{ url('admin/tags') }}" title="bookmark">Tags</a>

In table I have used action like below example

<a href="{{action('Admin\TagsController@show',['tag'=>$tag->id])}}" >View</a>

My question is how can I create both link without writing admin ? I have more then 100 unique link , I want to avoid to write this admin prefix every time in link.

Niloy Rony
  • 602
  • 1
  • 8
  • 23

1 Answers1

1

You may use route() helper function to generate url's.

/**
 * Generate the URL to a named route.
 *
 * @param  array|string  $name
 * @param  mixed  $parameters
 * @param  bool  $absolute
 * @return string
 */
function route($name, $parameters = [], $absolute = true)
{
    return app('url')->route($name, $parameters, $absolute);
}

You may name route your prefix/resource routes like showed in here and then use it in route function.

Route::namespace('Admin')->prefix('admin')->name('admin.')->group(function () {
     Route::resource('/tags', 'TagsController');
});

Please execute php artisan route:list command to see if you named the routes correctly.

Ersoy
  • 8,816
  • 6
  • 34
  • 48
  • In name why do need this dot ? – Niloy Rony Jun 06 '20 at 15:52
  • @NiloyRony it is just a convention - (the post i shared is old) - maybe in the newer versions of the laravel it doesn't require it but automatically appends it. – Ersoy Jun 06 '20 at 15:53
  • I have tried in version 7.x , without dot it's not working. – Niloy Rony Jun 06 '20 at 16:13
  • 1
    @NiloyRony if you put without dot then name shouldn’t include dot after it - you may need to clear routes and run route list to see what it prints and use the one listed in your view files – Ersoy Jun 06 '20 at 16:31