[英]laravel 5 nested routing issue
Laravel 5的新功能,並構建了一個小的Rest測試應用程序。 因此,我為單個控制器設想了2個不同的端點。
/myApp/public/index.php/states/{state}/cities //returns cities in a state
和
/myApp/public/index.php/states/{state}/cities/{city} //will do somethin else
我有點不清楚如何為此設置路線。 我想我可以讓這些端點使用相同的控制器方法,但是似乎更好的體系結構只是將每個端點路由到自己的方法。
到目前為止,我已經分別完成了兩件事,但不能一起工作:
在routes.php中
//route to the first endpoint
Route::resource('states.cities', 'StatesController');
//routes to the second endpoint if first is uncommented,otherwise blank page with no errors in log
Route::resource('states.cities', 'StatesController@cities');
和我的控制器代碼的相關部分:
class StatesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index(Request $request, $state)
{
//works
$cities = States::where('state', '=', $state)->lists('name');
echo json_encode($cities);
}
public function cities(Request $request, $state, $city)
{
echo "second request";
echo $state;
echo $city;
}
......
有人對處理此問題的正確方法有任何想法嗎? 干杯!
嘗試這個。
Route::get('states/{state}/cities', [
'as' => 'state.cities',
'uses' => 'StatesController@index'
]);
第二。
Route::get('states/cities/{state}/{city}', [
'as' => 'city.of.state',
'uses' => 'StatesController@cities'
]);
注意:在這種情況下,無需使用resource
路由。 resource
路由會創建您實際上不需要的不同路由的整個數組。 資源控制者
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.