猿问

管理员/仪表板定义路由未运行

两年后开始编程,从 zendframework2 切换到 laravel。我正在关注在早期版本的 Laravel 中制作的 youtube 教程。我在 web.php 中定义了一个简单的路由


这给了我一个管理/仪表板的工作网址


我一直在这个项目中使用它,直到我在我的 slider.blade.php 中使用它


路由管理/仪表板适用于 Request::is 但不适用于 href 路由。我收到未定义管理/仪表板的错误。但它在 web.php 和 php artisan route:list 中定义为 admin/dashboard。我还尝试在 web.php 中的 Route::group 之外定义 admin/dashboard 并使用 admin.dashboard 但我无法摆脱这个。有可能


网址:


<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::group(['middleware' => 'auth'], function () { 

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

});

网页.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');

        });


    });


慕少森
浏览 147回答 3
3回答

慕的地8271018

使用<a class="nav-link" href="/admin/dashboard">&nbsp;只能解决您的路由问题,但不能解决它。如果你按照上面的链接,它可以让你获得正确的视图,而 Laravel 不会抛出任何错误,那么你离解决路线问题又近了一步。这意味着存在具有该前端名称的路由。但是,根据我下面的解释,您会发现服务器端名称是admin.dashboardadmin.dashboard。所以,让我们开始解决问题。该as键不替代路线的名字,它结合了他们。因此,只使用其中一种方法 -内部路由定义中的as键或->name()方法。我正在 Laravel 5.8 上用我自己的机器对此进行测试,并且可以确认您可以在代码中同时使用两者。但是,您的代码中路由的名称是 - admin.dashboardadmin.dashboard - 令人惊讶。以下任一路由定义都可以解决问题://beneath is the route that can work both for auth and non auth for admin/Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {&nbsp; &nbsp; Route::group(['middleware' => 'auth'], function () {&nbsp; &nbsp; &nbsp; &nbsp; Route::get('dashboard', ['as' => 'dashboard', 'uses' => 'Admin\DashboardController@index']);&nbsp; &nbsp; &nbsp; &nbsp; Route::resource('slider', 'Admin\SliderController');&nbsp; &nbsp; });});或者//beneath is the route that can work both for auth and non auth for admin/Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {&nbsp; &nbsp; Route::group(['middleware' => 'auth'], function () {&nbsp; &nbsp; &nbsp; &nbsp; Route::get('dashboard', ['uses' => 'Admin\DashboardController@index'])->name('dashboard');&nbsp; &nbsp; &nbsp; &nbsp; Route::resource('slider', 'Admin\SliderController');&nbsp; &nbsp; });});无论哪种方式,生成的前端链接都应该是admin/dashboard而访问链接的服务器端路由是route('admin.dashboard')html 应该是:<li class="{{ Request::is('admin/dashboard*')? 'active':''}} ">&nbsp; &nbsp; <a class="nav-link" href="{{route('admin.dashboard')}}">&nbsp; &nbsp; &nbsp; &nbsp; <i class="material-icons">dashboard</i>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <p>Dashboard</p>&nbsp;&nbsp; &nbsp; </a>&nbsp;</li>注意:不要通过将生成的路由链接直接放入浏览器地址栏来测试它们。将它们放在一个链接中href,浏览器将绝对或相对地解析它们 - 取决于链接类型。

白衣染霜花

你必须在 Laravel 中的内部链接的“href”中的路由之前添加一个“/”。基本上你不需要在“href”中调用“route”方法,因为 Laravel 会自动确定它是哪条路由。尝试这个:<a&nbsp;class="nav-link"&nbsp;href="/admin/dashboard">

慕工程0101907

路由助手接受路由的名称,试试这个:href="{{&nbsp;route('admin.dashboard')&nbsp;}}https://laravel.com/docs/5.8/helpers#method-route
随时随地看视频慕课网APP
我要回答