Laravel5.3でのルーティングの変更

8/23にLaravel 5.3がリリースされたようです。

5.3を使ってみたのですが、appディレクトリ以下にroutes.phpが見つからなくて少し迷ったので、ルーティングを記述するファイルをメモしておきます。

ルーティングの記述場所

プロジェクトルート直下にroutesというディレクトリが追加になっていて、このディレクトリ内にあるweb.phpとapi.phpで行います。
基本的にはweb.phpにルーティングを記述して、APIの場合はapi.phpに記述する形のようです。

routes\web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

routes\api.php

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::get('/user', function (Request $request) {
    return $request->user();
})->middleware('auth:api');

web.phpに下記を追加してみると、/test で表示が確認できました。

routes\web.php

Route::get('test', function () {
    return 'ルーティングテスト';
});

 

【参考サイト】

 

このエントリーをはてなブックマークに追加

関連記事

コメントを残す

メールアドレスが公開されることはありません。
* が付いている欄は必須項目です

CAPTCHA


コメントが承認されるまで時間がかかります。

2024年4月
 123456
78910111213
14151617181920
21222324252627
282930