18-1
使用路由前置字串
一、路由前置字串
- 如果路由有一樣路徑的,可以利用前置字串
Route::prefix()
將之群組起來,例如:
Route::get('/exam/', 'ExamController@index')->name('exam.index');
Route::get('/exam/create', 'ExamController@create')->name('exam.create');
- 由於都有個
exam
,因此,可利用prefix('exam')
設定成:
Route::prefix('exam')->group(function () {
Route::get('/', 'ExamController@index')->name('exam.index');
Route::get('/create', 'ExamController@create')->name('exam.create');
});
- 因此,稍做整理,就會得到以下結果:
Route::prefix('exam')->group(function () {
Route::get('/', 'ExamController@index')->name('exam.index');
Route::get('/create', 'ExamController@create')->name('exam.create');
Route::post('', 'ExamController@store')->name('exam.store');
Route::get('/{exam}', 'ExamController@show')->name('exam.show');
Route::delete('/{exam}', 'ExamController@destroy')->name('exam.destroy');
Route::get('/{exam}/edit', 'ExamController@edit')->name('exam.edit');
Route::patch('/{exam}', 'ExamController@update')->name('exam.update');
});
Route::prefix('topic')->group(function () {
Route::post('/', 'TopicController@store')->name('topic.store');
Route::get('/{topic}/edit', 'TopicController@edit')->name('topic.edit');
Route::patch('/{topic}', 'TopicController@update')->name('topic.update');
Route::delete('/{topic}', 'TopicController@destroy')->name('topic.destroy');
});
Route::prefix('test')->group(function () {
Route::post('/', 'TestController@store')->name('test.store');
Route::get('/{test}', 'TestController@show')->name('test.show');
});
二、路由名稱前置字串
- 如果連路由名稱也想套用前置字串,那麼,只要在前置字串後面加一個
.
即可。
- 如果路由名稱也有一樣路徑的,可以利用前置字串
Route::name()
將之群組起來,例如:
Route::get('/exam/', 'ExamController@index')->name('exam.index');
Route::get('/exam/create', 'ExamController@create')->name('exam.create');
- 由於都有個
exam
,因此,可利用prefix('exam')
設定成:
Route::name('exam.')->group(function () {
Route::get('/exam', 'ExamController@index')->name('index');
Route::get('/exam/create', 'ExamController@create')->name('create');
});
三、同時使用路由前置字串及路由名稱前置字串
- 如果想要一起使用,那麼,稍微改變一下寫法即可,例如:
Route::group([
'prefix' => 'exam',
'as' => 'exam.',
], function () {
Route::get('/', 'ExamController@index')->name('index');
Route::get('/create', 'ExamController@create')->name('create');
Route::post('', 'ExamController@store')->name('store');
Route::get('/{exam}', 'ExamController@show')->name('show');
Route::delete('/{exam}', 'ExamController@destroy')->name('destroy');
Route::get('/{exam}/edit', 'ExamController@edit')->name('edit');
Route::patch('/{exam}', 'ExamController@update')->name('update');
});
Route::group([
'prefix' => 'topic',
'as' => 'topic.',
], function () {
Route::post('/', 'TopicController@store')->name('store');
Route::get('/{topic}/edit', 'TopicController@edit')->name('edit');
Route::patch('/{topic}', 'TopicController@update')->name('update');
Route::delete('/{topic}', 'TopicController@destroy')->name('destroy');
});
Route::group([
'prefix' => 'test',
'as' => 'test.',
], function () {
Route::post('/', 'TestController@store')->name('store');
Route::get('/{test}', 'TestController@show')->name('show');
});