我开始了一个新的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');
});
我可以通过直接在浏览器中粘贴链接来访问页面注册,而且当我运行工匠路线时,它会显示我创建的路线。在welcome.blade.php
中添加
{{link_to_route('signup')}}
并重新加载页面我有这个错误
ErrorException
Route [signup] not defined. (View: C:'wamp'www'atot'app'views'welcome.blade.php)
如何解决这个问题?
试试这个:
Route::any('signup', [
'as' => 'signup',
function() {
return View::make('signup');
}
]);
您的问题是您没有使用命名路由。
如果你愿意,你可以在这里阅读更多信息:http://laravel.com/docs/routing#named-routes
Link_to_route是一个方法,它生成一个url到给定的命名路由,所以要使它工作,你可以命名你的每条路由,然后它将工作
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')}}
您应该使用:
{{ link_to('signup') }}
或者使用名称
声明路由Route::any('signup', array('as' => 'signup', function()
{
// ...
}));
link_to_route
helper函数只对命名路由起作用,该路由在第一个参数中接受路由名称。