在 Laravel 4 安装中,使用 Jeffrey Way's Laravel 4 Generators ,我使用他的示例中的脚手架命令设置了一个“tweet”资源:
php artisan generate:scaffold tweet --fields="author:string, body:text"
这生成了推文类型的模型、 View 、 Controller 、迁移和路由信息。迁移数据库后,访问 http://localhost:8000/tweets
工作正常,并显示预期内容。
此时routes.php
文件的内容是:
Route::resource('tweets', 'TweetsController');
现在我想将 tweets
的 url 向上移动一级到 admin/tweets
中,因此上面的 url 应变为:http://localhost :8000/admin/tweets
。请注意,我并没有将“Admin”视为资源,而只是出于假设的组织目的而添加它。
将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 路由
显示命名路由现在也有 admin
前缀,将 tweets.create
转换为 admin .tweets.create
.
为什么错误提示找不到 tweets.create
?难道不应该自动解决(根据路由表判断),使用admin.tweets.create
吗?
如何更改路由以便不再出现此错误?
最佳答案
我刚刚使用新的资源 Controller 进行了测试,它对我来说工作得很好。
问题不在于路由,而在于应用程序中使用的命名路由
。
检查您的 View 文件,其中存在诸如link_to_route('tweets.create', 'Add new tweet')
之类的路由链接,这会产生错误,因为当您添加admin
作为前缀 tweets.create
不存在,因此将其更改为 admin.tweets.create
无论何时何地,在您的 controller
中使用命名路由。
关于Laravel 4 - 更改资源根路由路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18101438/