LoginSignup
4
1

More than 5 years have passed since last update.

laravel5.4のrouteでURLのルートパラメーターの文字によって出しわける

Posted at

はじめに

URLのルートパラメーターが特定の文字だったときにこのメソッドを返したい、その他は違うメソッドを返したい、ということがありました。
今回はそのルートパラメーターは必須パラーメーターとしています。

/routes/web.phpに書く

早速web.phpに書きます。

web.php
Route::get('/cat/{name}/', function (Request $request, $name) {
    if($name == 'bubbles'){
        return App\Http\Controllers\catController::myCat($request);
    }else{
        return App\Http\Controllers\catController::herCat($request);
    }
});

ルートパラメーターの{name}が'bubbles'の文字だったときはApp\Http\Controllers\catControllerのmyCatメソッドを、その他の文字だったときはApp\Http\Controllers\catControllerの'herCat'メソッドを返すという例です。

ルートパラメーターの{name}をfunctionで$nameとして使うことができるので、その中でif文で出し分ける、ということをしています。

4
1
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
4
1