未定义Laravel错误异常路由

时间:2014-03-16 17:55:53

标签: php laravel laravel-4

我开始一个由两个页面组成的新laravel项目make,我在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');
});

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

{{link_to_route('signup')}}

并重新加载我遇到此错误的页面

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

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

请改为尝试:

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

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

如果您愿意,可以在此处详细了解:http://laravel.com/docs/routing#named-routes

答案 1 :(得分:1)

Link_to_route是一种为给定的 named route 生成网址的方法,因此为了使其正常工作,您可以为每条路线命名,然后它就能正常工作

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

在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');
}));

然后您可以生成以下路线:

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

答案 2 :(得分:1)

你应该使用:

{{ link_to('signup') }}

或使用名称

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

link_to_route helper function仅适用于在第一个参数中接受路由名称的命名路由。