亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

目標類不存在。laravel 8 中的問題

目標類不存在。laravel 8 中的問題

PHP
慕少森 2024-01-19 15:32:32
當使用 laravel 8 創建新項目時,出現此錯誤。Illuminate\Contracts\Container\BindingResolutionException 目標類 [SayhelloController] 不存在。http://127.0.0.1:8000/users/john<?php    use Illuminate\Support\Facades\Route;     Route::get('/', function () {    return view('welcome');});      Route::get('/users/{name?}' , [SayhelloController::class,'index']);在 Laravel 文檔中,路由控制器類必須這樣定義 // Using PHP callable syntax...Route::get('/users', [UserController::class, 'index']);// Using string syntax...Route::get('/users', 'App\Http\Controllers\UserController@index');目標類別<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class SayhelloController extends Controller{    public function index($name = null)    {        return 'Hello '.$name;    }}所以我確實這么做了。
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

Laravel 8 更新了路由的編寫方式

在 laravel 8 中你需要使用像

use?App\Http\Controllers\SayhelloController;
Route::get('/users/{name?}'?,?[SayhelloController::class,'index']);

或者

Route::get('/users',?'App\Http\Controllers\UserController@index');

如果你想使用舊的方式

然后在RouteServiceProvider.php

添加這一行

?/**

? ? ?* This namespace is applied to your controller routes.

? ? ?*

? ? ?* In addition, it is set as the URL generator's root namespace.

? ? ?*

? ? ?* @var string

? ? ?*/

? ? protected $namespace = 'App\Http\Controllers'; // need to add in Laravel 8

? ??


public function boot()

{

? ? $this->configureRateLimiting();


? ? $this->routes(function () {

? ? ? ? Route::prefix('api')

? ? ? ? ? ? ->middleware('api')

? ? ? ? ? ? ->namespace($this->namespace) // need to add in Laravel 8

? ? ? ? ? ? ->group(base_path('routes/api.php'));


? ? ? ? Route::middleware('web')

? ? ? ? ? ? ->namespace($this->namespace) // need to add in Laravel 8

? ? ? ? ? ? ->group(base_path('routes/web.php'));

? ? });

}

然后你可以使用像


Route::get('/users/{name?}' , [SayhelloController::class,'index']);

Route::resource('/users' , SayhelloController::class);

或者


Route::get('/users', 'UserController@index');


查看完整回答
反對 回復 2024-01-19
  • 1 回答
  • 0 關注
  • 238 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號