Laravel 8:ReflectionException 函数()不存在
Posted
技术标签:
【中文标题】Laravel 8:ReflectionException 函数()不存在【英文标题】:Laravel 8: ReflectionException Function () does not exist 【发布时间】:2021-10-23 03:26:29 【问题描述】:我是一名初学者,正在学习如何使用 Laravel 8。 当我尝试执行我的代码时,我得到了错误:ReflectionException Function() 不存在。
我已经参考了 Laravel 文档#Writing Controllers, #Basic Controllers 部分的语法,但问题无法解决。
造成这个问题的原因是什么以及如何解决?
以下代码是我在 PurchaseController.php 中的控制器部分:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class PurchasesController extends Controller
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function index()
return view('purchases.index');
public function purchase()
//
以下部分是我在web.php中的路由
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PurchaseController;
/*
|--------------------------------------------------------------------------
| 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('/purchases', [PurchaseController::class], 'index');
Route::post('/purchases', [PurchaseController::class], 'purchase');
Route::get('/', function ()
return view('welcome');
);
更新: 根据下面提供的答案,我已将我的代码 web.app 代码更改为以下代码。但是,我收到另一条错误消息: Illuminate\Contracts\Container\BindingResolutionException 目标类 [App\Http\Controllers\PurchasesController] 不存在。
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PurchasesController;
/*
|--------------------------------------------------------------------------
| 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('/purchases', [PurchasesController::class, 'index']);
Route::post('/purchases', [PurchasesController::class , 'purchase']);
Route::get('/', function ()
return view('welcome');
);
我更新 PurchaseController.php 代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class PurchasesController extends Controller //Change the naming as suggestion below
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function index()
return view('purchases.index');
public function purchase()
//
【问题讨论】:
路由不正确 【参考方案1】:你应该这样写你的路线:
并将 PurchaseController 更改为 PurchasesController
Route::get('/purchases', [PurchasesController::class, 'index']);
Route::post('/purchases', [PurchasesController::class, 'purchase']);
和
use App\Http\Controllers\PurchasesController;
【讨论】:
我试过这个,得到错误:Illuminate\Contracts\Container\BindingResolutionException 目标类 [PurchaseController] 不存在。我认为“使用 App\Http\Controllers\PurchasesController;”这行不通,因为我的文件名是 PurchaseController.php 你有一个命名问题,如果你的类名是 PurchaseController 然后去修复类本身:class PurchasesController extends Controller 根据您的建议,我将类名更改为 PurchasesController 扩展控制器,但仍然出现错误。 Illuminate\Contracts\Container\BindingResolutionException 目标类 [App\Http\Controllers\PurchasesController] 不存在。我已确保保存更改,然后重新运行“php artisan serve”。 很奇怪试试这个:Route::get('/purchases', [App\Http\Controllers\PurchasesController::class, 'index']); Route::post('/purchases', [App\Http\Controllers\PurchasesController::class, 'purchase']); 让我们continue this discussion in chat。【参考方案2】:=> 打开 App\Providers\RouteServiceProvider.php 并取消注释这一行在 laravel 8 中试试这个
受保护的 $namespace = 'App\Http\Controllers';
【讨论】:
以上是关于Laravel 8:ReflectionException 函数()不存在的主要内容,如果未能解决你的问题,请参考以下文章
如何将 App\Exceptions 从 laravel 7 升级到 laravel 8
在 Laravel 8 中安装 Laravel/ui 的问题
Laravel 图像规则验证不适用于 Laravel 8,但适用于 Laravel 7 |拉拉维尔 |图片 |验证