0

我目前正在构建一个 CMS 系统,我为其创建了一组路由,该组以“cms”为前缀。问题出在我的 menu.blade 文件中,我在其中使用超链接 HREF 属性中的前缀调用了路由并得到了以下错误。

路线 [cms/welcome] 未定义。(查看:C:\Bitnami\wampstack-7.1.27-0\apache2\htdocs\nathanreynolds\resources\views\layouts\CMS\menu.blade.php)

我可以通过在浏览器顶部输入 url 来访问路由就好了。但是当使用{{route('cms/welcome')}}调用路由时,它会返回此错误。

menu.blade.php

<ul id="CMSnav">
 <li><a href="{{route('cms/welcome')}}">Welcome</a></li>
 <li><a href="{{route('cms/about')}}">About</a></li>
 <li><a href="{{route('cms/contact')}}">Contact us</a></li>
</ul>

web.php(路由)

/** CMS routes **/
Route::group(['prefix'=>'cms','middleware'=>'auth'],function(){
  Route::get('welcome','cmsController@index');
  Route::put('updateLayouts','cmsController@updateLayouts');
  Route::get('about','cmsController@about');
  Route::get('contact','cmsController@contact');
  //Route::resource('/CMS','cmsController');
});

通过php artisan route:list打印路线列表

C:\Bitnami\wampstack-7.1.27-0\apache2\htdocs\nathanreynolds>php artisan route:list
+--------+---------------+-------------------------------------------+-----------------------------------+---------------------------------------------------------------------------+------------------+
| Domain | Method        | URI                                       | Name                              | Action                                                                    | Middleware       |
+--------+---------------+-------------------------------------------+-----------------------------------+---------------------------------------------------------------------------+------------------+
|        | GET|HEAD      | /                                         |                                   | Closure                                                                   | web              |
|        | GET|HEAD      | Blogs                                     | Blogs                             | App\Http\Controllers\pagescontroller@Blogs                                | web,Closure      |
|        | GET|HEAD      | CMS/about                                 |                                   | App\Http\Controllers\cmsController@about                                  | web,auth,Closure |
|        | GET|HEAD      | CMS/contact                               |                                   | App\Http\Controllers\cmsController@contact                                | web,auth,Closure |
|        | PUT           | CMS/updateLayouts                         |                                   | App\Http\Controllers\cmsController@updateLayouts                          | web,auth,Closure |
|        | GET|HEAD      | CMS/welcome                               |                                   | App\Http\Controllers\cmsController@index
4

1 回答 1

1

帮助程序用于为命名路由生成 URL,这route不是您的情况。你有两种选择来完成你想要做的事情:

#1 改用url助手:

<li><a href="{{url('/cms/welcome')}}">Welcome</a></li>

#2 改用命名路由。因此,在您的路线文件中,路线将被命名为:

Route::get('welcome','cmsController@index')->name('cms.welcome');

然后你可以使用route助手:

<li><a href="{{route('cms.welcome')}}">Welcome</a></li>
于 2019-04-17T13:44:58.333 回答