路由时如何区分参数与声明的路由?

时间:2016-04-28 21:05:34

标签: php routes laravel-5.2

拥有下一条路线:

Route::get('/apartment/{apartment_name}', 'ApartmentController@getApartmentByName');

Route::get('/apartment/create', [
            'uses' => 'ApartmentController@create',
            'as'   => 'apartment.create'
        ]);

Route::get('/apartment/edit', [
            'uses' => 'ApartmentController@edit',
            'as'   => 'apartment.edit',
        ]);

我怎样才能在路线之间产生影响 myapp.com/apartment/create myapp.com/apartment/beach-apartment

我想使用相同的URI前缀(apartment /)搜索公寓的名称,但是使用此代码我总是调用参数路径。

1 个答案:

答案 0 :(得分:1)

这是因为无论是被调用,创建还是编辑,都在参数之一/apartment/{apartment_name}内匹配,createedit等于apartment_name

只需将参数1移动到该块中最下面的一行。

Route::get('/apartment/create', [
            'uses' => 'ApartmentController@create',
            'as'   => 'apartment.create'
        ]);

Route::get('/apartment/edit', [
            'uses' => 'ApartmentController@edit',
            'as'   => 'apartment.edit',
        ]);

Route::get('/apartment/{apartment_name}', 'ApartmentController@getApartmentByName');

使用此配置,如果/apartment/create/apartment/edit未匹配,则它将匹配/apartment/{apartment_name}