30
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel5.4で使えるルーティングチートシート

Last updated at Posted at 2017-02-26

ファイル分割

routes/user.phproutes/admin.phpでルーティングファイルを分ける例

app/Providers/RouteServiceProvider.php

  public function map()
  {
    $this->mapUserRoutes();
  
    $this->mapAdminRoutes();
  }

  protected function mapUserRoutes()
  {
    Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/user.php'));
  }
  
  protected function mapAdminRoutes()
  {
    Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/admin.php'));
  }

middleware

Route::group(['middleware' => 'auth'], function() {
  Route::get('/', 'HomeController@index');
  Route::get('hoge', 'HomeController@hoge');
});

namespace

Route::group(['namespace' => 'Admin'], function() {
  // app/Http/Controllers/Admin/HomeController.php
  Route::get('/', 'HomeController@index');
});

prefix

Route::group(['prefix' => 'admin'], function() {
  // /admin にapp/Http/Controllers/HomeController.phpを割り当てる
  Route::get('/', 'HomeController@index');
});

サブドメインルーティング

Route::group(['domain' => '{account}.myapp.com'], function () {
  Route::get('user/{id}', function ($account, $id) {
    //
  });
});

name

Route::get('/', 'HomeController@index')->name('home');

resource

Route::resource('hoge', 'Hoge\MainController');

[option] only

indexとshowしかいらない場合

Route::resource('hoge', 'Hoge\MainController', ['only' => ['index', 'show']]);

[option] except

indexとshowはいらない場合

Route::resource('hoge', 'Hoge\MainController', ['except' => ['index', 'show']]);

[option] names

あんまり扱わないけど、Hoge\MainController@indexのルーティング名(hoge.index)をfugaに変更したい場合

Route::resource('hoge', 'Hoge\MainController', ['names' => ['index' => 'fuga']]);

[option] as

prefixnamespaceと合わせて使う場面が多い。resourceで名前にprefixをつける。
下記はadmin.hoge.index, admin.hoge.show...(以下略)となる。

Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {  
  Route::resource('hoge', 'Hoge\MainController', ['as' => 'admin']);
});

HTTP METHOD

// テストに出るやつ
Route::get('/', 'Hoge\MainController@index');
Route::post('store', 'Hoge\MainController@store');
Route::patch('update', 'Hoge\MainController@update');
Route::delete('destroy', 'Hoge\MainController@destroy');
Route::options('foo', 'Hoge\MainController@foo');
Route::match(['get', 'post'], 'bar', 'Hoge\MainController@bar');

// あまり使わない
Route::put('fuga', 'Hoge\MainController@fuga');
Route::any('bar', 'Hoge\MainController@bar');

パラメータ

Route::get('/users/{id}', 'User\MainController@show');

コントローラはこんな感じ

app/Http/Controller/User/MainController.php
public function show($id)
{
  //
}

任意パラメータ

Route::get('/users/{name?}', 'User\MainController@show');

パラメータの制約

Route::get('/users/{id}/{name}', 'User\MainController@show')->where(
  ['id' => '[0-9]+', 'name' => '[a-z]+']
);

モデルとの結合

API作るときに便利そう

Route::get('api/users/{user}', function (App\User $user) {
    return $user->email;
});

デフォルトはid検索のため、変えたい場合はモデルに記述する

public function getRouteKeyName()
{
  return 'name';
}

参考

30
39
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?