php Laravel 5 路由未定义,而它是?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28714675/
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 5 route not defined, while it is?
提问by Ben Fransen
I'm a little confused on how this is supposed to work. But I'm getting an Route [/preferences/1] not defined
error.
我对这应该如何工作感到有些困惑。但我收到一个Route [/preferences/1] not defined
错误。
In my routes.php I have:
在我的 routes.php 我有:
Route::patch('/preferences/{id}', 'UserController@update');
Route::patch('/preferences/{id}', 'UserController@update');
And in the view file (account/preferences.blade.php) I have:
在视图文件 (account/preferences.blade.php) 中,我有:
{!! Form::model(Auth::user(), ['method' => 'PATCH', 'route' => '/preferences/' . Auth::user()->id]) !!}
{!! Form::model(Auth::user(), ['method' => 'PATCH', 'route' => '/preferences/' . Auth::user()->id]) !!}
I'm getting an error telling me the route doesn't exist. I think I'm misunderstanding the docs on this topic but in my opinion I've defined a route for PATCH requests with a given parameter, and set this in the view correctly.
我收到一条错误消息,告诉我该路线不存在。我想我误解了关于这个主题的文档,但在我看来,我已经用给定的参数为 PATCH 请求定义了一个路由,并在视图中正确设置了它。
What am I overlooking here?
我在这里俯瞰什么?
回答by Joel Hinz
The route()
method, which is called when you do ['route' => 'someroute']
in a form opening, wants what's called a named route. You give a route a name like this:
该route()
方法在您['route' => 'someroute']
打开表单时调用,需要称为命名路由。你给一个路由起这样的名字:
Route::patch('/preferences/{id}',[
'as' => 'user.preferences.update',
'uses' => 'UserController@update'
]);
That is, you make the second argument of the route into an array, where you specify both the route name (the as
), and also what to do when the route is hit (the uses
).
也就是说,您将路由的第二个参数放入一个数组中,您可以在其中指定路由名称 (the as
),以及当路由被命中时要执行的操作 (the uses
)。
Then, when you open the form, you call the route:
然后,当你打开表单时,你调用路由:
{!! Form::model(Auth::user(), [
'method' => 'PATCH',
'route' => ['user.preferences.update', Auth::user()->id]
]) !!}
Now, for a route without parameters, you could just do 'route' => 'routename'
, but since you have a parameter, you make an array instead and supply the parameters in order.
现在,对于没有参数的路由,您可以只执行'route' => 'routename'
,但是由于您有一个参数,因此您可以创建一个数组并按顺序提供参数。
All that said, since you appear to be updating the current user's preferences, I would advise you to let the handling controller check the id of the currently logged-in user, and base the updating on that - there's no need to send in the id in the url and the route unless your users should need to update the preferences of other users as well. :)
综上所述,由于您似乎正在更新当前用户的首选项,我建议您让处理控制器检查当前登录用户的 id,并以此为基础进行更新 - 无需发送 id在 url 和路由中,除非您的用户还需要更新其他用户的首选项。:)
回答by Bantu
This thread is old but was the first one to come up so i thought id share my solution too. Apart from having named routes in your routes.php file. This error can also occur when you have duplicate URLs in your routes file, but with different names the error can be misleading in this scenario. Example
这个线程很旧,但它是第一个出现的,所以我想我也分享我的解决方案。除了在你的 routes.php 文件中有命名路由。当您的路由文件中有重复的 URL 时,也会发生此错误,但名称不同,在这种情况下该错误可能会产生误导。例子
Route::any('official/form/reject-form}', 'FormStatus@rejectForm')->name('reject-form');
Route::any('official/form/accept-form', 'FormStatus@acceptForm')->name('reject-form');
Changing on of the names solves the problem. Copy pasting and fatigue will get you to this problem :).
更改名称可以解决问题。复制粘贴和疲劳会让你遇到这个问题:)。
回答by ALeX inSide
If route is not defined, then check web.php routing file.
如果没有定义路由,则检查 web.php 路由文件。
Route::get('/map', 'NavigationController@map')->name('map'); // note the name() method.
Then you can use this method in the views:
然后你可以在视图中使用这个方法:
<a class="nav-link" href="{{ route('map') }}">{{ __('Map') }}</a>
PS: the __('Map') is to translate "Map" to the current language.
PS:__('Map') 是将“Map”翻译成当前语言。
And the list of names for routes you can see with artisan command:
您可以使用 artisan 命令查看路线名称列表:
php artisan route:list
回答by JustCarty
I'm using Laravel 5.7 and tried all of the above answers but nothing seemed to be hitting the spot.
我正在使用 Laravel 5.7 并尝试了上述所有答案,但似乎没有任何效果。
For me, it was a rather simple fix by removing the cache files created by Laravel.
It seemed that my changes were not being reflected, and therefore my application wasn't seeing the routes.
对我来说,删除 Laravel 创建的缓存文件是一个相当简单的修复。
似乎我的更改没有得到反映,因此我的应用程序没有看到路由。
A bit overkill, but I decided to reset all my cache at the same time using the following commands:
有点矫枉过正,但我决定使用以下命令同时重置所有缓存:
php artisan route:clear
php artisan view:clear
php artisan cache:clear
The main one here is the first command which will delete the bootstrap/cache/routes.php
file.
The second command will remove the cached files for the views that are stored in the storage/framework/cache
folder.
Finally, the last command will clear the application cache.
这里的主要命令是第一个删除bootstrap/cache/routes.php
文件的命令。
第二个命令将删除存储在storage/framework/cache
文件夹中的视图的缓存文件。
最后,最后一个命令将清除应用程序缓存。
回答by DEEPAK
when you execute the command
当你执行命令时
php artisan route:list
You will see all your registered routes in there in table format . Well there you see many columns like Method , URI , Name , Action .. etc.
您将在那里以表格格式看到所有已注册的路线。好吧,您会看到许多列,例如 Method 、 URI 、 Name 、 Action .. 等。
So basically if you are using route()method that means it will accept only namecolumn values and if you want to use URIcolumn values you should go with url()method of laravel.
所以基本上如果你使用route()方法,这意味着它只接受name列值,如果你想使用URI列值,你应该使用laravel 的url()方法。
回答by entoniperez
My case is a bit different, since it is not a form but to return a view. Add method ->name('route')
.
我的情况有点不同,因为它不是表单而是返回视图。添加方法->name('route')
。
MyView.blade.php
looks like this:
MyView.blade.php
看起来像这样:
<a href="{{route('admin')}}">CATEGORIES</a>
And web.php
routes file is defined like this:
和web.php
路线文件的定义如下:
Route::view('admin', 'admin.index')->name('admin');
回答by Ilyas Kuzu
i had the same issue and find the solution lately.
我有同样的问题,最近找到了解决方案。
you should check if your route is rather inside a route::group
您应该检查您的路线是否位于路线::组内
like here:
像这儿:
Route::group(['prefix' => 'Auth', 'as' => 'Auth.', 'namespace' => 'Auth', 'middleware' => 'Auth']
if so you should use it in the view file. like here:
如果是这样,您应该在视图文件中使用它。像这儿:
!! Form::model(Auth::user(), ['method' => 'PATCH', 'route' => 'Auth.preferences/' . Auth::user()->id]) !!}
回答by Peter
In my case the solution was simple:
就我而言,解决方案很简单:
I have defined the route at the very start of the route.php
file.
我已经在route.php
文件的开头定义了路由。
After moving the named route to the bottom, my app finally saw it. It means that somehow the route was defined too early.
将命名路由移到底部后,我的应用程序终于看到了。这意味着不知何故,路线定义得太早了。
回答by Adiyya Tadikamalla
One more cause for this:
造成这种情况的另一个原因:
If the routes are overridden with the same URI(Unknowingly), it causes this error:
如果路由被相同的 URI(不知不觉)覆盖,则会导致此错误:
Eg:
例如:
Route::get('dashboard', ['uses' => 'SomeController@index', 'as' => 'my.dashboard']);
Route::get('dashboard/', ['uses' => 'SomeController@dashboard', 'as' => 'my.home_dashboard']);
In this case route 'my.dashboard' is invalidate as the both routes has same URI ('dashboard', 'dashboard/')
在这种情况下,路由“my.dashboard”无效,因为两条路由具有相同的 URI(“dashboard”、“dashboard/”)
Solution: You should change the URI for either one
解决方案:您应该更改任何一个的 URI
Eg:
例如:
Route::get('dashboard', ['uses' => 'SomeController@index', 'as' => 'my.dashboard']);
Route::get('home-dashboard', ['uses' => 'SomeController@dashboard', 'as' => 'my.home_dashboard']);
// See the URI changed for this 'home-dashboard'
// 查看为这个“home-dashboard”更改的 URI
Hope it helps some once.
希望它能帮助一些人。
回答by Adeel Raza Azeemi
Please note that the command
请注意该命令
php artisan route:list
Or to get more filter down list
或获取更多过滤列表
php artisan route:list | grep your_route|your_controller
the forth column tells you the names of routes that are registered (usually generated by Route::resource)
第四列告诉您已注册的路由名称(通常由 Route::resource 生成)