4
1

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 3 years have passed since last update.

Laravel8のルーティング

Last updated at Posted at 2020-11-09

はじめに

laravelの8でこれまでのバージョンとルーティングの書き方が変わったようなので、変更点を記載する。

前提

PostControllerのindexアクションを呼びたい時

PostController.php
<?php

namespace App\Http\Controllers;

use App\Models\Post;

/**
 * Class PostController
 * @package App\Http\Controllers
 */
class PostController extends Controller
{
    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $posts = Post::all();
        return view('post.index', ['posts' => $posts]);
    }

}

laravel8より前

web.php
<?php
use Illuminate\Support\Facades\Route;

Route::get('/posts', 'PostController@index');

laravel8

web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

Route::get('/posts', [PostController::class, 'index']);

まとめ

laravel8からはコントローラーをweb.phpでuseしてあげないといけなくなったみたいですね。
明示的にどのコントローラーが使われているのかが分かるけど、記述量が増えちゃいますね。
これは賛否両論ありそう。

参考

laravelドキュメント

4
1
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?