LoginSignup
0
0

More than 1 year has passed since last update.

Laravel8 大きい変更点(ルーティング)

Last updated at Posted at 2022-05-22

ふい、豚野郎です。
今日はLaravel8の大きい変更点について書いていきます。
個人的にルーティングが大きいと思うので見て行ってください。
おまけとして、CSRF対策も書いていきます。
(環境構築は事前に済ませてください)

参考:https://e-seventh.com/laravel8-call-controller/
使わせていただきます。

1. ルーティング

以下が従来までのルーティングファイルの記述です。

routes/web.php
<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

// 従来の書き方
Route::get('/', 'FormController@index')->name('form');
Route::post('/store', 'FormController@store')->name('store');

以下のエラーが出ます。
screencapture-localhost-8080-Laravel8-public-2022-05-22-13_32_01.png

Laravel8では、以下の2パターンになりました。
①. 名前空間を書くパターン

routes/web.php
<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', 'App\Http\Controllers\FormController@index')->name('form');
Route::post('/store', 'App\Http\Controllers\FormController@store')->name('store');

②. useするパターン

routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FormController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', [FormController::class, 'index'])->name('form');
Route::post('/store', [FormController::class, 'store'])->name('store');

2. CSRF対策(いつからこうなったかわからない)

resources/views/form.blade.php
<html>

<head>
    <title>テストフォーム</title>
</head>

<body>
    <form action="{{ route('store') }}" method="post">
        @csrf
        <label for="email">テスト</label>
        <input type="text" name="test">
        <input type="submit">
    </form>
</body>

</html>

いつから@csrfになったんだろ。
Laravelから離れている期間があったので知りませんでした。
6くらいには使われているみたいです。
ちなみに、以下(古い方)でもCSRF対策はできます。

resources/views/form.blade.php
<html>

<head>
    <title>テストフォーム</title>
</head>

<body>
    <form action="{{ route('store') }}" method="post">
        {{ csrf_field() }}
        <label for="email">テスト</label>
        <input type="text" name="test">
        <input type="submit">
    </form>
</body>

</html>
app/Http/Controllers/FormController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FormController extends Controller
{
    // フォーム
    public function index() {
        return view('form');
    }

    // 登録(今回はCSRFトークンの確認のため、登録しない)
    public function store(Request $req) {
        dd($req->all());
    }
}

dd()で確認しましょう。
screencapture-localhost-8080-Laravel8-public-2022-05-22-13_52_27.png
送信クリック
screencapture-localhost-8080-Laravel8-public-store-2022-05-22-13_52_37.png

ちなみに、LaravelはCSRF対策をしないと、

419
PAGE EXPIRED

となるため、注意が必要です。

0
0
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
0
0