在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
?
如何更改我的路由,以便不再發生此錯誤?
我剛剛測試過新的資源控制器,它對我來說工作正常。問題不在於路由,它與視圖文件檢查您的視圖文件有鏈接路由,如'link_to_route('tweets.create','添加新的推文'),這是因爲當你添加'admin'作爲前綴'tweets.create'不存在,所以將它改爲'admin.tweets.create',在你的控制器中,用於 –
感謝您的快速回復!我其實自己也注意到了這一點。在發佈問題之前應該看起來更長一些:P – Johannes