2021年07月02日
Target class [△△△△Controller] does not exist.のエラー|Laravel学習
のんびりさんは現在Laravelの勉強しています
渋谷で働くエンジニア福さんのLaravel入門で一覧画面を表示しようとしたらエラーが出ました
Laravelのバージョンの違うので、自分のバージョンにあった記述に修正。
備忘録としてまとめます。
OS:Windows10
PHP:PHP 7.4.20
Laravel:Laravel Framework 8.48.1
MySQL:MySQL Server 8.0 (Ver 8.0.23 for Win64 on x86_64 (MySQL Community Server - GPL))
Node.js:14.17.20
npm:6.14.13
渋谷で働くエンジニア福さんのLaravel入門で一覧画面を表示しようとしたらエラーが出ました
Laravelのバージョンの違うので、自分のバージョンにあった記述に修正。
備忘録としてまとめます。
開発環境
OS:Windows10
PHP:PHP 7.4.20
Laravel:Laravel Framework 8.48.1
MySQL:MySQL Server 8.0 (Ver 8.0.23 for Win64 on x86_64 (MySQL Community Server - GPL))
Node.js:14.17.20
npm:6.14.13
もくじ
- BlogController.phpの記述
- web.phpの記述
- RouteServiceProvider.phpの修正
-
no image
-
no image
BlogController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BlogController extends Controller
{
/*
* ブログ一覧を表示する
*
* @return view
*/
public function showList()
{
return view('blog.list');
}
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BlogController extends Controller
{
/*
* ブログ一覧を表示する
*
* @return view
*/
public function showList()
{
return view('blog.list');
}
}
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('/', 'BlogController@showList')->name('blogs');
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('/', 'BlogController@showList')->name('blogs');
RouteServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/home';
/**
* The controller namespace for the application.
*
* When present, controller route declarations will automatically be prefixed with this namespace.
*
* @var string|null
*/
// protected $namespace = 'App\\Http\\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
protected $namespace = 'App\Http\Controllers'; //追加
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
}
}
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/home';
/**
* The controller namespace for the application.
*
* When present, controller route declarations will automatically be prefixed with this namespace.
*
* @var string|null
*/
// protected $namespace = 'App\\Http\\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
protected $namespace = 'App\Http\Controllers'; //追加
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
}
}
protected $namespace = 'App\Http\Controllers';を36行目に追記
出典:渋谷で働くエンジニア福の「実践で学ぶプログラミング入門」
(https://www.youtube.com/channel/UCqzJi2o7bAPtRD3tTFvi7zA)
【このカテゴリーの最新記事】
この記事へのコメント
コメントを書く
この記事へのトラックバックURL
https://fanblogs.jp/tb/10831505
※ブログオーナーが承認したトラックバックのみ表示されます。
この記事へのトラックバック