我尝试创建到我的 index.blade.php
的路线页,我使用 cmd php artisan make:controller ProductController
创建了一个 Controller “ProductController” ,所以在 http --> Controller 中我有一个 ProductController.php 文件,我把这段代码放在里面:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductContoller extends Controller
{
public function index()
{
return view('products.index');
}
}
然后在我的 web.php
我使用此代码创建了一条路线Route::get('/boutique', 'ProductController@index');
但是它不起作用。首先,当我使用 Laragon->
Projetname.test
访问我在 localhost 上为我的项目设置的漂亮 url 时我得到了正常的 Laravel 欢迎页面,但是当我尝试转到我刚刚设置的 url 时:ProjectName.test/boutique
,我明白了"Target class [App\Http\Controllers\ProductController] does not exist."
阅读 Laravel 更新到 V8 后的变化 here ,我已经看到更新对路由提出了一些要求,因为 $namespace 前缀没有自动启用,但是可以通过在
RouteServiceProvider
中取消注释这一行来再次启用它。// protected $namespace = 'App\\Http\\Controllers';
我取消注释该行,然后使用 php artisan route:cache
清除缓存,但它仍然无法正常工作..当我第一次开始研究 Laravel 中的路由问题时,我看到许多论坛发现 httpd.config 文件中的 apache Allowoverride 设置可能会导致问题,因此我将其设置从
None
更改为至 All
然后重新启动 Laragon 但没有任何效果。在我的 Controller 上更正标签后它仍然不起作用,我尝试了两种方法(旧的和新的)它们都不适合我,cmd 不断返回我:
λ php artisan route:list
Illuminate\Contracts\Container\BindingResolutionException
Target class [ProductController] does not exist.
at C:\laragon\www\ProjectName\vendor\laravel\framework\src\Illuminate\Container\Container.php:835
831▕
832▕ try {
833▕ $reflector = new ReflectionClass($concrete);
834▕ } catch (ReflectionException $e) {
➜ 835▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
836▕ }
837▕
838▕ // If the type is not instantiable, the developer is attempting to resolve
839▕ // an abstract type such as an Interface or Abstract Class and there is
1 [internal]:0
Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))
2 C:\laragon\www\ProjectName\vendor\laravel\framework\src\Illuminate\Container\Container.php:833
ReflectionException::("Class "ProductController" does not exist")
最佳答案
Laravel 8 路线应该是
Route::get('/boutique', [NameController:class,'method']);
所以在你的 web.php 文件中添加 use App\Http\Controllers\ProductContoller
然后像这样写你的路线:Route::get('/boutique', [ProductContoller::class,'index']);
而且我认为缺少' r ' 在您的“ ProductController ”类名中
关于php - 尽管在 RouteServiceProvider 中取消了注释,但 Controller 仍无法在 Laravel 8 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66960224/