Laravel 5.1 UrlGenerator 路由未定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33424794/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:35:57  来源:igfitidea点击:

Laravel 5.1 UrlGenerator route not defined error

laravellaravel-5routinglaravel-5.1laravel-routing

提问by Shannon Phillips

I have an app built with Laravel 5.1. I'm using Form::open using a route_name to generate the url to post the form to as well as creating links by defining route and using the UrlGenerator. The issue is that I have some buttons/links created for display purposes and do not have pages created yet.

我有一个用 Laravel 5.1 构建的应用程序。我正在使用 Form::open 使用 route_name 生成 url 以将表单发布到以及通过定义路由和使用 UrlGenerator 创建链接。问题是我创建了一些用于显示目的的按钮/链接,但还没有创建页面。

I get a route not defined error with stack trace going back to UrlGenerator line 296. I'm wanting to set up something so that the error does not display. Instead, I would like a link to be generated to the pre-defined page that I have created saying that the feature the user clicked on is not yet developed.

我收到一个路由未定义错误,堆栈跟踪返回到 UrlGenerator 第 296 行。我想设置一些东西,以便不显示错误。相反,我希望生成一个指向我创建的预定义页面的链接,说明用户单击的功能尚未开发。

I thought about doing something similar to a 404 error, but the issue is that the existing page (the page the link or button lives on) is not being displayed, not just that route is missing.

我想过做一些类似于 404 错误的事情,但问题是现有页面(链接或按钮所在的页面)没有显示,而不仅仅是缺少该路由。

For example, below, I create a link to the route "broker_contact_create" Since this route does not exist, the page displaying the link will not load. Instead, I get the error saying:

例如,在下面,我创建了一个指向“broker_contact_create”路由的链接,由于该路由不存在,因此不会加载显示该链接的页面。相反,我收到错误消息:

ErrorException in UrlGenerator.php line 296: Route [broker_contacts_create] not defined. (View: index.blade.php)

UrlGenerator.php 第 296 行中的 ErrorException:未定义路由 [broker_contacts_create]。(查看:index.blade.php)

<div class="col-md-6 col-lg-7 margin-bottom-15">
    <a href="{{ route('broker_contacts_create') }}" class="btn btn-success btn-icon-plus">+ Add Contact</a>

</div>

Instead, I want the page to be displayed. When the user clicks on the link to a missing route, to have them routed to a page that tells the user they clicked on a link to a feature that has not been enabled yet.

相反,我希望显示页面。当用户点击缺少路线的链接时,将他们路由到一个页面,告诉用户他们点击了一个尚未启用的功能的链接。

So basically I just want it to do: if route not found then provide $url.

所以基本上我只想这样做:如果找不到路由,则提供 $url。

采纳答案by andrewtweber

If you follow the stack trace, you'll see that the routefunction basically invokes UrlGenerator->route

如果您遵循堆栈跟踪,您将看到该route函数基本上调用UrlGenerator->route

https://github.com/laravel/framework/blob/5.1/src/Illuminate/Routing/UrlGenerator.php#L300

https://github.com/laravel/framework/blob/5.1/src/Illuminate/Routing/UrlGenerator.php#L300

Which checks if the route with that name exists, and if not, throws an InvalidArgumentException.

它检查具有该名称的路由是否存在,如果不存在,则抛出一个InvalidArgumentException.

Basically, what you're trying to do is not possible. You mustdefine that route if you want to use the routefunction.

基本上,您尝试做的事情是不可能的。你必须,如果你想使用定义的路由route功能。

I think your best option is to set up a "feature not developed" view, and point all of these pending routes to that view. That way you can use the routefunction. Your link/button will be generated, but will take them to a "not yet developed" page. Another benefit to this is that all of your routes are laid out and you can easily see which ones need to be developed.

我认为您最好的选择是设置一个“未开发的功能”视图,并将所有这些待处理的路由指向该视图。这样您就可以使用该route功能。您的链接/按钮将生成,但会将它们带到“尚未开发”页面。这样做的另一个好处是您的所有路线都已布置好,您可以轻松查看需要开发哪些路线。

Route::get('/brokers/contacts/create', ['uses' => 'HomeController@notDeveloped', 'as' => 'broker_contact_create']);

And inside your HomeController:

在你的 HomeController 里面:

public function notDeveloped() {
    return view('pages.not_developed');
}