Search code examples
controllerrouteslaravel-bladelaravel-5.5

How to use prefixed route in blade?


I have following routes:

Auth::routes();
Route::get('/' , 'HomeController@index')->name('mainHomePage');

Route::prefix('admin')->group(function () {
    Route::get('login' , 'admin\AdminController@login')->name('admin.login');
    Route::resource('/','admin\AdminController');
    Route::resource('subjects','admin\SubjectsController');
});

I'm unable to get the route of my subjects in blade syntax. I have working url http://localhost/quizl/admin/subjects . But I get error when I try to get same route using "{{route('admin.subjects')}}" or "{{route('admin.subjects.index')}}" any where in blade file. UPDATE The error is:

Parse error: syntax error, unexpected 'admin' (T_STRING), expecting ',' or ')' (View: /var/www/html/quizl/resources/views/admin/header.blade.php) (View: /var/www/html/quizl/resources/views/admin/header.blade.php) (View: /var/www/html/quizl/resources/views/admin/header.blade.php) enter image description here

How to do this? I did not find much help in this context in internet.


Solution

  • Most of the time you can simply use whatever name you assigned to the route. For example, {{route('admin.login')}}

    However, when you group routes you have the option to "prefix the name" on all child routes like this:

    Route::prefix('admin')->name('admin.')->group(function(){
        Route::get('/login' , 'AdminController@login');
        Route::resource('/','AdminController@index');
        Route::resource('/subjects','SubjectsController@subjects')->name('subjects');
    }
    

    Note the period after the name -- this will allow you to use dot notation to get the route named subjects from within the admin group like

    {{route('admin.subjects)}}
    

    The error in above code was simply {{route('admin.subjects'))}} was simply extra closing parenthesis.