:::

18-1 使用路由前置字串

一、路由前置字串

  1. 如果路由有一樣路徑的,可以利用前置字串Route::prefix()將之群組起來,例如:
    Route::get('/exam/', 'ExamController@index')->name('exam.index');
    Route::get('/exam/create', 'ExamController@create')->name('exam.create');
    
  2. 由於都有個exam,因此,可利用prefix('exam')設定成:
    Route::prefix('exam')->group(function () {
        Route::get('/', 'ExamController@index')->name('exam.index');
        Route::get('/create', 'ExamController@create')->name('exam.create');
    });
  3. 因此,稍做整理,就會得到以下結果:
    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');
    });
    

二、路由名稱前置字串 

  1. 如果連路由名稱也想套用前置字串,那麼,只要在前置字串後面加一個.即可。
  2. 如果路由名稱也有一樣路徑的,可以利用前置字串Route::name()將之群組起來,例如:
    Route::get('/exam/', 'ExamController@index')->name('exam.index');
    Route::get('/exam/create', 'ExamController@create')->name('exam.create');
    
  3. 由於都有個exam,因此,可利用prefix('exam')設定成:
    Route::name('exam.')->group(function () {
        Route::get('/exam', 'ExamController@index')->name('index');
        Route::get('/exam/create', 'ExamController@create')->name('create');
    });

三、同時使用路由前置字串及路由名稱前置字串 

  1. 如果想要一起使用,那麼,稍微改變一下寫法即可,例如:
    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');
    });

到GitHub觀看此單元程式異動


:::

書籍目錄

展開 | 闔起

快速登入


https%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1257%26tbsn%3D37

計數器

今天: 8589858985898589
昨天: 2489248924892489
總計: 8030571803057180305718030571803057180305718030571