0
votes

I have started programming after two years and switched from zendframework2 to laravel. i am following a youtube tutorial which is made in the earlier version of laravel. i have defined a simple route in web.php

which gives me a working url of admin/dashboard

i have been using it in this project until i used it in my slider.blade.php

the route admin/dashboard works with Request::is but does not work with href route. i get an error of admin/dashboard is not defined. but it is defined in web.php and also in php artisan route:list as admin/dashboard. i have also tried to define admin/dashboard outside Route::group in web.php and using admin.dashboard but i can't get out of this. it might be

Html:

<li class="{{ Request::is('admin/dashboard*')? 'active':''}} ">
    <a class="nav-link" href="{{route('admin/dashboard')}}"> <i class="material-icons">dashboard</i> <p>Dashboard</p> </a> 
</li>

Route:

Route::group(['middleware' => 'auth'], function () { 
    Route::get('dashboard', ['as' => 'dashboard', 'uses' => 'Admin\DashboardController@index'])->name('admin.dashboard'); 
});

web.php:

    <?php

    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */

    Route::get('/', function () {
        return view('welcome');
    });

    Auth::routes();


    //beneath is the route that can work both for auth and non auth for admin/
    Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {


        Route::group(['middleware' => 'auth'], function () {
            Route::get('dashboard', ['as' => 'dashboard', 'uses' => 'Admin\DashboardController@index'])->name('admin.dashboard');
            Route::resource('slider', 'Admin\SliderController');
        });

    });

4
<li class="{{ Request::is('admin/dashboard*')? 'active':''}} "> <a class="nav-link" href="{{route('admin/dashboard')}}"> <i class="material-icons">dashboard</i> <p>Dashboard</p> </a> </li> - Seeker
Route::group(['middleware' => 'auth'], function () { Route::get('dashboard', ['as' => 'dashboard', 'uses' => 'Admin\DashboardController@index'])->name('admin.dashboard'); }); - Seeker
stackoverflow did not let me add code to the question so i had to add it here, sorry - Seeker
Can you add your web.php file code here? - Dilip Hirapara
Seeker, click edit to edit your question and add the code that you put into the comments. - Dave

4 Answers

2
votes

Using

<a class="nav-link" href="/admin/dashboard"> 

only troubleshoots your routing problem but doesn't solve it.

If you follow the above link and it gets you the correct view without Laravel throwing any errors then you are one step close to solving the route issue. It means there is a route with that front-end name. However, from my explanation below you find out that the server-side name is admin.dashboardadmin.dashboard.

So, let's go about solving the problem.

The as key does not replace your route name, it combines them. So, only use one of the methods - the as key or the ->name() method in the inner route definition.

I'm testing this with my own machine on Laravel 5.8 and can confirm that you can use both as in your code. However, the name for the route in your code is - admin.dashboardadmin.dashboard - surprisingly.

Either of the route definitions below should solve the problem:

//beneath is the route that can work both for auth and non auth for admin/
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {

    Route::group(['middleware' => 'auth'], function () {

        Route::get('dashboard', ['as' => 'dashboard', 'uses' => 'Admin\DashboardController@index']);

        Route::resource('slider', 'Admin\SliderController');
    });

});

OR

//beneath is the route that can work both for auth and non auth for admin/
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {

    Route::group(['middleware' => 'auth'], function () {

        Route::get('dashboard', ['uses' => 'Admin\DashboardController@index'])->name('dashboard');

        Route::resource('slider', 'Admin\SliderController');
    });

});

Either ways, the generated front-end link should be

admin/dashboard

And the server-side route to access the link is

route('admin.dashboard')

The html should be:

<li class="{{ Request::is('admin/dashboard*')? 'active':''}} ">
    <a class="nav-link" href="{{route('admin.dashboard')}}">
        <i class="material-icons">dashboard</i> 
        <p>Dashboard</p> 
    </a> 
</li>

Note: do not test generated route links by putting them directly into the browser address bar. Put them in a link href, the browser will resolve them either absolutely or relatively - depending on the link type.

2
votes

You have to add a "/" before routes in "href" for internal links in Laravel. and basically you don't need to call "route" method in "href" because Laravel automatically determine which route it's. try this:

<a class="nav-link" href="/admin/dashboard"> 
0
votes

The route helper accepts the name of a route, try this:

href="{{ route('admin.dashboard') }}

https://laravel.com/docs/5.8/helpers#method-route

0
votes

The route that you define:

Route::group(['middleware' => 'auth'], function () { 
Route::get('dashboard', [
        'as' => 'dashboard', 
        'uses' => 'Admin\DashboardController@index'
    ])->name('admin.dashboard'); 
});

produce /dashboard link and route('admin.dashboard') named route which both are not being used. The solution is can be either using /dashboard as link and route('admin.dashboard') for named route as below:

<li class="{{ Request::is('/dashboard')? 'active':''}} ">
    <a class="nav-link" href="{{route('admin.dashboard')}}">
        <i class="material-icons">dashboard</i> <p>Dashboard</p>
    </a> 
</li> 

assuming your group route have no prefix.