Laravel Route - 没有中间页

时间:2015-05-31 23:01:59

标签: php laravel routes resources

我有两个控制器 - ChController& FillController。 ChController工作正常。

Route::resource('c', 'ChController');

    |        | GET|HEAD | c                | c.index         | App\Http\Controllers\ChController@index     |            |
    |        | GET|HEAD | c/create         | c.create        | App\Http\Controllers\ChController@create    |            |
    |        | POST     | c                | c.store         | App\Http\Controllers\ChController@store     |            |
    |        | GET|HEAD | c/{c}            | c.show          | App\Http\Controllers\ChController@show      |            |
    |        | GET|HEAD | c/{c}/edit       | c.edit          | App\Http\Controllers\ChController@edit      |            |
    |        | PUT      | c/{c}            | c.update        | App\Http\Controllers\ChController@update    |            |
    |        | PATCH    | c/{c}            |                 | App\Http\Controllers\ChController@update    |            |
    |        | DELETE   | c/{c}            | c.destroy       | App\Http\Controllers\ChController@destroy   |            |

但我需要嵌套的FillController(f),如:

/c/{c}/create
/c/{c}/{f}
/c/{c}/{f}/show
/c/{c}/{f}/edit

// etc...

我试过了:

Route::group(['prefix' => 'c/{c}'], function () {
    Route::resource('{f}', 'FillController');
}); 

    |        | GET|HEAD | c/{c}/{f}            | c.{c}.{f}.index   | App\Http\Controllers\FillController@index   |            |
    |        | GET|HEAD | c/{c}/{f}/create     | c.{c}.{f}.create  | App\Http\Controllers\FillController@create  |            |
    |        | POST     | c/{c}/{f}            | c.{c}.{f}.store   | App\Http\Controllers\FillController@store   |            |
    |        | GET|HEAD | c/{c}/{f}/{{f}}      | c.{c}.{f}.show    | App\Http\Controllers\FillController@show    |            |
    |        | GET|HEAD | c/{c}/{f}/{{f}}/edit | c.{c}.{f}.edit    | App\Http\Controllers\FillController@edit    |            |
    |        | PUT      | c/{c}/{f}/{{f}}      | c.{c}.{f}.update  | App\Http\Controllers\FillController@update  |            |
    |        | PATCH    | c/{c}/{f}/{{f}}      |                   | App\Http\Controllers\FillController@update  |            |
    |        | DELETE   | c/{c}/{f}/{{f}}      | c.{c}.{f}.destroy | App\Http\Controllers\FillController@destroy |            |


Route::group(['prefix' => 'c/{c}'], function () {
    Route::resource('f', 'FillController');
}); 

    |        | GET|HEAD | c/{c}/f          | c.{c}.f.index   | App\Http\Controllers\FillController@index   |            |
    |        | GET|HEAD | c/{c}/f/create   | c.{c}.f.create  | App\Http\Controllers\FillController@create  |            |
    |        | POST     | c/{c}/f          | c.{c}.f.store   | App\Http\Controllers\FillController@store   |            |
    |        | GET|HEAD | c/{c}/f/{f}      | c.{c}.f.show    | App\Http\Controllers\FillController@show    |            |
    |        | GET|HEAD | c/{c}/f/{f}/edit | c.{c}.f.edit    | App\Http\Controllers\FillController@edit    |            |
    |        | PUT      | c/{c}/f/{f}      | c.{c}.f.update  | App\Http\Controllers\FillController@update  |            |
    |        | PATCH    | c/{c}/f/{f}      |                 | App\Http\Controllers\FillController@update  |            |
    |        | DELETE   | c/{c}/f/{f}      | c.{c}.f.destroy | App\Http\Controllers\FillController@destroy |            |

所有这些都是中间页/ f /,我不需要。

最接近的是:

Route::group(['prefix' => 'c/{c}'], function () {
    Route::resource('', 'FillController');
});

    |        | GET|HEAD | c/{c}/create  | c.{c}..create  | App\Http\Controllers\FillController@create  |            |
    |        | POST     | c/{c}         | c.{c}..store   | App\Http\Controllers\FillController@store   |            |
    |        | GET|HEAD | c/{c}/{}      | c.{c}..show    | App\Http\Controllers\FillController@show    |            |
    |        | GET|HEAD | c/{c}/{}/edit | c.{c}..edit    | App\Http\Controllers\FillController@edit    |            |
    |        | PUT      | c/{c}/{}      | c.{c}..update  | App\Http\Controllers\FillController@update  |            |
    |        | PATCH    | c/{c}/{}      |                | App\Http\Controllers\FillController@update  |            |
    |        | DELETE   | c/{c}/{}      | c.{c}..destroy | App\Http\Controllers\FillController@destroy |            |

但是现在路由器还不知道{f}参数。

1 个答案:

答案 0 :(得分:0)

首先,当您引用它时,f不是中间页,它只是一个URI段。这是一种方法,因为你清楚地说明正在访问什么子资源(f)。我强烈建议你坚持下去。

现在,如果您仍想继续前进,可以通过一个例外来实现它。在命名方案中,index子资源不能有f路由,因为它会覆盖show资源的c

Route::group(['prefix' => 'c/{c}'], function () {
    // You can't have this because it will overwrite c.show
    // Route::get('/', ['as' => 'f.index', 'uses' => 'FillController@index']);
    Route::get('/create', ['as' => 'f.create', 'uses' => 'FillController@create']);
    Route::post('/', ['as' => 'f.store', 'uses' => 'FillController@store']);
    Route::get('/{f}/edit', ['as' => 'f.edit', 'uses' => 'FillController@edit']);
    Route::put('/{f}', ['as' => 'f.update', 'uses' => 'FillController@update']);
    Route::patch('/{f}', 'FillController@update');
    Route::delete('/{f}', ['as' => 'f.destroy', 'uses' => 'FillController@destroy']);
});
|   | GET|HEAD | c              | c.index   | App\Http\Controllers\ChController@index     |     |
|   | GET|HEAD | c/create       | c.create  | App\Http\Controllers\ChController@create    |     |
|   | POST     | c              | c.store   | App\Http\Controllers\ChController@store     |     |
|   | GET|HEAD | c/{c}          | f.index   | App\Http\Controllers\FillController@index   |     |
|   | GET|HEAD | c/{c}/edit     | c.edit    | App\Http\Controllers\ChController@edit      |     |
|   | PUT      | c/{c}          | c.update  | App\Http\Controllers\ChController@update    |     |
|   | PATCH    | c/{c}          |           | App\Http\Controllers\ChController@update    |     |
|   | DELETE   | c/{c}          | c.destroy | App\Http\Controllers\ChController@destroy   |     |
|   | GET|HEAD | c/{c}/create   | f.create  | App\Http\Controllers\FillController@create  |     |
|   | POST     | c/{c}          | f.store   | App\Http\Controllers\FillController@store   |     |
|   | GET|HEAD | c/{c}/{f}/edit | f.edit    | App\Http\Controllers\FillController@edit    |     |
|   | PUT      | c/{c}/{f}      | f.update  | App\Http\Controllers\FillController@update  |     |
|   | PATCH    | c/{c}/{f}      |           | App\Http\Controllers\FillController@update  |     |
|   | DELETE   | c/{c}/{f}      | f.destroy | App\Http\Controllers\FillController@destroy |     |