Laravel で提供されている様々なルーティング方法を紹介します。
- クロージャを使ったルーティング
- ルートパラメータ
- コントローラへのルーティング
- RESTful リソースコントローラ
- 暗黙コントローラ
- 名前付きルート
クロージャーを使ったルーティング
// app/Http/routes.php Route::get('foo/bar', function() { return 'GET: foo/bar'; }); Route::post('foo/bar', function() { return 'POST: foo/bar'; }); Route::put('foo/bar', function() { return 'PUT: foo/bar'; }); Route::delete('foo/bar', function() { return 'DELETE: foo/bar'; });
artisan コマンドでルートを確認してみます。
php artisan route:list
+--------+----------+---------+------+---------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+---------+------+---------+------------+ | | GET|HEAD | foo/bar | | Closure | | | | POST | foo/bar | | Closure | | | | PUT | foo/bar | | Closure | | | | DELETE | foo/bar | | Closure | | +--------+----------+---------+------+---------+------------+
ルートパラメータ
URIからパラメータを取得することが出来ます。
// app/Http/routes.php Route::get('foo/{id}', function($id) { return "パラメータ: $id"; });
パラメータをオプションにすることができます。その場合は受け取り側で初期値を設定します。
Route::get('foo/{id?}', function($id = '初期値') { return "パラメータ: $id"; });
正規表現を使って、パラメータの書式を定義することができます。
Route::get('foo/{id}', function($id) { return "パラメータ $id"; })->where('id', '[0-9]+');
正規表現を配列にして、複数指定することができます。
Route::get('foo/{id}/{name}', function($id, $name) { return "パラメータ $id, $name"; })->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
コントローラへのルーティング
// app/Http/routes.php Route::get('articles', 'ArticlesController@index'); Route::get('articles/{id}', 'ArticlesController@show'); Route::get('articles/create', 'ArticlesController@create'); Route::post('articles', 'ArticlesController@store'); Route::get('articles/{id}/edit', 'ArticlesController@edit'); Route::put('articles/{id}', 'ArticlesController@update'); Route::delete('articles/{id}', 'ArticlesController@delete');
+--------+----------+--------------------+------+------------------------------------------------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+--------------------+------+------------------------------------------------+------------+ | | GET|HEAD | articles | | App\Http\Controllers\ArticlesController@index | | | | GET|HEAD | articles/{id} | | App\Http\Controllers\ArticlesController@show | | | | GET|HEAD | articles/create | | App\Http\Controllers\ArticlesController@create | | | | POST | articles | | App\Http\Controllers\ArticlesController@store | | | | GET|HEAD | articles/{id}/edit | | App\Http\Controllers\ArticlesController@edit | | | | PUT | articles/{id} | | App\Http\Controllers\ArticlesController@update | | | | DELETE | articles/{id} | | App\Http\Controllers\ArticlesController@destroy | | +--------+----------+--------------------+------+------------------------------------------------+------------+
RESTful Resouce Controller
// app/Http/routes.php Route::resource('articles', 'ArticlesController');
+--------+----------+--------------------------+------------------+-------------------------------------------------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+--------------------------+------------------+-------------------------------------------------+------------+ | | GET|HEAD | articles | articles.index | App\Http\Controllers\ArticlesController@index | | | | GET|HEAD | articles/create | articles.create | App\Http\Controllers\ArticlesController@create | | | | POST | articles | articles.store | App\Http\Controllers\ArticlesController@store | | | | GET|HEAD | articles/{articles} | articles.show | App\Http\Controllers\ArticlesController@show | | | | GET|HEAD | articles/{articles}/edit | articles.edit | App\Http\Controllers\ArticlesController@edit | | | | PUT | articles/{articles} | articles.update | App\Http\Controllers\ArticlesController@update | | | | PATCH | articles/{articles} | | App\Http\Controllers\ArticlesController@update | | | | DELETE | articles/{articles} | articles.destroy | App\Http\Controllers\ArticlesController@destroy | | +--------+----------+--------------------------+------------------+-------------------------------------------------+------------+
リソースアクションの一部のみを取り扱うこともできます。
Route::resource('articles', 'ArticlesController', ['only' => ['index', 'show']]); Route::resource('articles', 'ArticlesController', ['except' => ['create', 'store', 'edit', 'update', 'destroy']]);
リソースをネストさせることもできます。ネストさせる時はドットで区切ります。
Route::resource('articles.comments', 'CommentsController');
+--------+----------+----------------------------------------------+---------------------------+-------------------------------------------------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+----------------------------------------------+---------------------------+-------------------------------------------------+------------+ | | GET|HEAD | articles/{articles}/comments | articles.comments.index | App\Http\Controllers\CommentsController@index | | | | GET|HEAD | articles/{articles}/comments/create | articles.comments.create | App\Http\Controllers\CommentsController@create | | | | POST | articles/{articles}/comments | articles.comments.store | App\Http\Controllers\CommentsController@store | | | | GET|HEAD | articles/{articles}/comments/{comments} | articles.comments.show | App\Http\Controllers\CommentsController@show | | | | GET|HEAD | articles/{articles}/comments/{comments}/edit | articles.comments.edit | App\Http\Controllers\CommentsController@edit | | | | PUT | articles/{articles}/comments/{comments} | articles.comments.update | App\Http\Controllers\CommentsController@update | | | | PATCH | articles/{articles}/comments/{comments} | | App\Http\Controllers\CommentsController@update | | | | DELETE | articles/{articles}/comments/{comments} | articles.comments.destroy | App\Http\Controllers\CommentsController@destroy | | +--------+----------+----------------------------------------------+---------------------------+-------------------------------------------------+------------+
Implicit Controller
Implicit Controller(暗黙のコントローラ)では、コントローラのメソッド名からルートのURIを決定します。
// app/Http/Controllers/HogesController.php class HogesController extends Controller { public function getIndex() { // } public function postProfile() { // } }
Route::controller('hoge', 'HogesController');
+--------+--------------------------------+-----------------------------------------------------+------+----------------------------------------------------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+--------------------------------+-----------------------------------------------------+------+----------------------------------------------------+------------+ | | GET|HEAD | hoge/index/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\HogesController@getIndex | | | | GET|HEAD | hoge | | App\Http\Controllers\HogesController@getIndex | | | | POST | hoge/profile/{one?}/{two?}/{three?}/{four?}/{five?} | | App\Http\Controllers\HogesController@postProfile | | | | GET|HEAD|POST|PUT|PATCH|DELETE | hoge/{_missing} | | App\Http\Controllers\HogesController@missingMethod | | +--------+--------------------------------+-----------------------------------------------------+------+-------------------------------------------------+------------+
—
Named Route
URLをより便利に生成するために、ルートに名前を付けることができます。
Route::get('user/profile', ['as' => 'profile', function() { // }]);
+--------+----------+--------------+---------+---------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+--------------+---------+---------+------------+ | | GET|HEAD | user/profile | profile | Closure | | +--------+----------+--------------+---------+---------+------------+
tihnker でURLの生成を試してみます。
$ php artisan tinker >>> url('user/profile'); => "http://localhost/user/profile" >>> >>> route('profile'); => "http://localhost/user/profile" >>>
パスからURLを生成するときは url() を使うのですが、ルート名を設定してある時は route() を使って、URLを生成することが可能になります。
ルート名はコントローラのアクションに対しても付けることができます。
Route::get('articles', ['as' => 'article_list', 'uses' => 'ArticlesController@index']);
+--------+----------+----------+--------------+-----------------------------------------------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+----------+--------------+-----------------------------------------------+------------+ | | GET|HEAD | articles | article_list | App\Http\Controllers\ArticlesController@index | | +--------+----------+----------+--------------+-----------------------------------------------+------------+
まとめ
代表的なルーティングを紹介しましたが、ルートのグルーピング等、ここには掲載しきれていない物もあります。公式サイトに目を通しておくことをお薦めします。なお、公式サイトでのルーティングの説明はルーティングのページ意外にコントローラーのところにも説明がありますので、そちらも合わせてご覧ください。
http://laravel.com/docs/5.1/routing
http://laravel.com/docs/5.1/controllers