Niloy Rony
Niloy Rony

Reputation: 632

Laravel create link for prefix action

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.

Upvotes: 1

Views: 1066

Answers (1)

Ersoy
Ersoy

Reputation: 9596

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.

Upvotes: 1

Related Questions