在Laravel 4安装中,使用Jeffrey Way's Laravel 4 Generators,我设置了一条推文'资源,使用他的例子中的scaffolding命令:
php artisan generate:scaffold tweet --fields="author:string, body:text"
这生成了推文类型的模型,视图,控制器,迁移和路由信息。迁移数据库后,访问http://localhost:8000/tweets
工作正常,并显示预期的内容。
此时routes.php
文件的内容为:
Route::resource('tweets', 'TweetsController');
现在,我想将tweets
的网址向上移动到admin/tweets
,因此上面的网址应该变为:http://localhost:8000/admin/tweets
。请注意,我不会对待管理员'作为一种资源,而只是想将其添加到假设的组织目的。
将routes.php文件更改为:
Route::resource('admin/tweets', 'TweetsController');
不起作用,并显示以下错误:
无法生成指定路线的网址" tweets.create"因为这样的路线不存在。
同样使用以下内容时:
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
?
如何更改路由以便不再出现此错误?
答案 0 :(得分:2)
我刚刚测试了新的资源控制器,它对我来说很好。
问题不在于Route,而是在您的应用程序中使用named routes
。
检查您的视图文件是否有指向link_to_route('tweets.create', 'Add new tweet')
之类的路由的链接,这会产生错误,因为当您添加admin
作为前缀tweets.create
时不存在,请将其更改为{{ 1}} admin.tweets.create
中的每个地方,也都使用了命名路线。