:::

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觀看此單元程式異動


:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D37%26tbdsn%3D1257

計數器

今天: 1427142714271427
昨天: 1416141614161416
總計: 7340074734007473400747340074734007473400747340074