php 未定义路由的 Laravel 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21623400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel link to route not defined
提问by DolDurma
I'm grouping profilecontroller and I want to link to that. Then I define this route:
我正在分组profile控制器,我想链接到它。然后我定义了这条路线:
//Group to put all the routes that need login first
Route::group(array('prefix'=> 'admin', 'before' => 'csrf'), function(){
Route::resource('/profile' , 'ProfileController', array('as'=>'profile') );
});
and this is my menu link:
这是我的菜单链接:
<li><a href="{{ URL::route('admin.profile') }}">profile Managment</a></li>
and this it my result of routein terminal:
这是我route在终端的结果:
+--------+----------------------------------+------------------------+---------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+----------------------------------+------------------------+---------------------------+----------------+---------------+
| | GET / | index | Closure | | |
| | GET admin/index | dashboard | Closure | | |
| | GET logout | logout | Closure | | |
| | POST auth | auth | Closure | csrf | |
| | GET login | login | Closure | | |
| | GET admin/profile | admin..profile.index | ProfileController@index | csrf | |
| | GET admin/profile/create | admin..profile.create | ProfileController@create | csrf | |
| | POST admin/profile | admin..profile.store | ProfileController@store | csrf | |
| | GET admin/profile/{profile} | admin..profile.show | ProfileController@show | csrf | |
| | GET admin/profile/{profile}/edit | admin..profile.edit | ProfileController@edit | csrf | |
| | PUT admin/profile/{profile} | admin..profile.update | ProfileController@update | csrf | |
| | PATCH admin/profile/{profile} | | ProfileController@update | csrf | |
| | DELETE admin/profile/{profile} | admin..profile.destroy | ProfileController@destroy | csrf | |
+--------+----------------------------------+------------------------+---------------------------+----------------+---------------+
Now I get this error:
现在我收到这个错误:
ErrorException
Route [admin.profile] not defined. (View: /var/www/alachiq/app/views/back_end/menu.blade.php) (View: /var/www/alachiq/app/views/back_end/menu.blade.php) (View: /var/www/alachiq/app/views/back_end/menu.blade.php)
回答by Simo A.
Remove the /character from your Route::resourcemethod. It is causing the double dots, which in turn are causing your error message.
/从您的Route::resource方法中删除字符。它导致双点,这反过来又导致您的错误消息。
Should be:
应该:
Route::resource('profile' , 'ProfileController', array('as'=>'profile') );
Route::resource('profile' , 'ProfileController', array('as'=>'profile') );
Either format (/profileor profile) would usually work, but when using the prefixoption with Route::groupyou need to remove the /from resource URL.
格式 (/profile或profile) 通常都可以使用,但是当使用该prefix选项时,Route::group您需要/从资源 URL 中删除。
EDIT:Also it seems to me that you should be pointing your link to route admin.profile.index, not admin.profile.
编辑:在我看来,您应该将链接指向 route admin.profile.index,而不是admin.profile.
回答by Craftein
Why don't you just do URL::to('admin/profile');
你为什么不做 URL::to('admin/profile');
Since what you're trying to achieve with URL::route('admin.profile');is almost the same number of types away.
由于您要实现的目标URL::route('admin.profile');几乎是相同数量的类型。
Now from what I understand, URL::route('profile');will produce a full URL string to a a route with the same name you assigned to.
现在,据我所知,URL::route('profile');将生成一个完整的 URL 字符串到与您分配的名称相同的路由。
EDIT
编辑
echo URL::route('admin.profile.index');
Should work. Based from Docs, you should include .index under ROUTE NAME.
应该管用。基于Docs,您应该在 .index 下包含 .index ROUTE NAME。
回答by Nima
Solution For Laravel 5.6 and above
Laravel 5.6 及以上解决方案
First step: do php artisan route:listin the terminal of your IDE.
第一步:php artisan route:list在 IDE 的终端中执行。
Second step: find the admin/profile in the URI column.
第二步:在 URI 列中找到 admin/profile。
Third step: in the Name column related to that uri you should find some thing like
profile.index
第三步:在与该 uri 相关的 Name 列中,您应该找到类似
profile.index
Last Step: You should use name provided in third step in your href.
最后一步:您应该在您的 href 中使用第三步中提供的名称。
Note: Contrary to laravel 5.3 in laravel 5.6 and above using admin.profile.indexwould not work
注意:与 laravel 5.6 及以上版本中的 laravel 5.3 相反,使用admin.profile.index是行不通的

