ペチパーノート

WEB開発系Tipsブログです。

Laravelのルーティング設定

様々なルーティング設定

こんなコントローラがある想定

Controllers
 └PersonController.php

■よくあるルート

Route::get('person/edit', 'PersonController@edit');
Route::post('person/edit', 'PersonController@update’);

■名前付きルート ルート設定に名前をつけられる。
リダイレクトなどのときに便利らしい

Route::get(‘person/add', 'PersonController@add’)->name(‘personadd’)

■条件付きルート
正規表現でパラメータを制限できる

Route::get(‘hello/{id}’, 'HelloController@index’)->where(‘id’, ‘[0-9]+’);

■ルートグループ
ルートをグルーピングできる。

などができるっぽい

名前空間とグループルート

Controllers
 └Sample
   └SampleController.php

だとしたら

Route::get(’sample/edit', 'Sample/SampleController@edit’);
Route::post(’sample/edit', 'Sample/SampleController@update’);

と書かなきゃいけないが、グループ化するとこのように記述できる

Route::namespace(’Sample’)->group(function() {
  Route::get(’sample/edit', 'SampleController@edit’);
  Route::post(’sample/edit', 'SampleController@update’);  
})