Laravel 错误异常路由未定义

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

Laravel error exception route not defined

phplaravellaravel-4

提问by Mohamed Bouallegue

I start a new laravel project make composed by two pages, I created the pages under the app/viewdirectory and this is my route.phpfile:

我开始了一个由两个页面组成的新 Laravel 项目,我在app/view目录下创建了页面,这是我的route.php文件:

Route::get('/', function()
{
    return View::make('hello');
});

Route::get('welcome', function()
{
    return View::make('welcome');
});

Route::any('signup', function()
{
    return View::make('signup');
});

I can access to the pages signup by taping the link directly in the browser and also when I run artisan routes it shows me the routes that I created. in the welcome.blade.phpwhen I add the line

我可以通过直接在浏览器中点击链接来访问页面注册,并且当我运行 artisan routes 时,它会显示我创建的路由。在welcome.blade.php我添加行时

{{link_to_route('signup')}}

and reload the page I have this error

并重新加载页面我有这个错误

ErrorException
Route [signup] not defined. (View: C:\wamp\www\atot\app\views\welcome.blade.php)

how can I solve this problem?

我怎么解决这个问题?

回答by kajetons

Try this instead:

试试这个:

Route::any('signup', [
    'as' => 'signup',
    function() {
        return View::make('signup');
    }
]);

Your problem was that you didn't use a named route.

您的问题是您没有使用命名路线。

If you want, you can read more about it here: http://laravel.com/docs/routing#named-routes

如果您愿意,可以在此处阅读更多相关信息:http: //laravel.com/docs/routing#named-routes

回答by marcanuy

Link_to_route is a method that generates a url to a given named route, so to make it work you can name each of your routes and then it will work

Link_to_route 是一种为给定命名路由生成 url 的方法,因此要使其工作,您可以命名每个路由,然后它就会工作

  link_to_route('route.name', $title, $parameters = array(), $attributes = array());

In routes.php update the following

在 routes.php 中更新以下内容

Route::get('/', array('as'=>'home', function()
{
    return View::make('hello');
}));

Route::get('welcome', array('as'=>'welcome', function()
{
    return View::make('welcome');
}));

Route::any('signup', array('as'=>'signup', function()
{
    return View::make('signup');
}));

Then you can generate the following routes:

然后你可以生成以下路由:

{{link_to_route('home')}}
{{link_to_route('welcome')}}
{{link_to_route('signup')}}

回答by The Alpha

You should use either:

您应该使用:

{{ link_to('signup') }}

Or declare the route using a name

或使用名称声明路线

Route::any('signup', array('as' => 'signup', function()
{
    // ...
}));

The link_to_routehelper functiononly works with a named route which accepts a route name in the first argument.

link_to_route辅助功能只适用于它接受的第一个参数的路径名命名的路线。