2013-08-07 142 views
1

在Laravel 4的安装,使用Jeffrey Way's Laravel 4 Generators,我成立“鸣叫”资源,使用所述脚手架命令从他的例子:Laravel 4 - 变化的资源根路由路径

php artisan generate:scaffold tweet --fields="author:string, body:text" 

此产生的模型,视图,控制器,推送类型的迁移和路由信息。迁移数据库后,访问http://localhost:8000/tweets工作正常,并显示预期的内容。

routes.php文件在这一点上的内容是:

Route::resource('tweets', 'TweetsController'); 

现在我想移动的URL tweets一个级别为admin/tweets,因此上述网址应该变成:http://localhost:8000/admin/tweets。请注意,我并不是将“管理员”视为资源,而是只是为了虚构的组织目的而添加它。

的routes.php文件文件更改为:

Route::resource('admin/tweets', 'TweetsController'); 

不工作,并显示以下错误:

Unable to generate a URL for the named route "tweets.create" as such route does not exist.

同样使用以下时:

Route::group(array('prefix' => 'admin'), function() { 
    Route::resource('tweets', 'TweetsController'); 
}); 

由于建议在this stackoverflow question

使用php artisan routes显示指定的路线现在也有admin作为其前缀,将tweets.create转换为admin.tweets.create


为什么错误说它找不到tweets.create?不应该自动解决(通过航线表判断),使用admin.tweets.create

如何更改我的路由,以便不再发生此错误?

+1

我刚刚测试过新的资源控制器,它对我来说工作正常。问题不在于路由,它与视图文件检查您的视图文件有链接路由,如'link_to_route('tweets.create','添加新的推文'),这是因为当你添加'admin'作为前缀'tweets.create'不存在,所以将它改为'admin.tweets.create',在你的控制器中,用于 –

+0

感谢您的快速回复!我其实自己也注意到了这一点。在发布问题之前应该看起来更长一些:P – Johannes

回答

2

我刚刚测试过新的资源控制器,它对我来说工作正常。 问题不在路由中,而是在应用程序中使用named routes

检查你的视图文件有链接到路线像link_to_route('tweets.create', 'Add new tweet'),这是产生错误,因为当你添加admin作为前缀tweets.create不存在那么将其更改为admin.tweets.create每一个地方,在你的controller也究竟在哪儿命名的路线是用过的。

+0

出于好奇,有没有更好的方式来链接视图中的路由,如果添加像这样的前缀,这些路由不会中断?可能使用link_to_action? – Johannes

+1

是最好的选择'link_to_action('TweetsController @ create','添加新推文')' –