我试图创建到index.blade.php
页面的路由,我制作了一个控制器"ProductController";使用cmdphp artisan make:controller ProductController
,因此在http->控制器我确实有一个ProductController.php文件,我在其中放了以下代码:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class ProductContoller extends Controller
{
public function index()
{
return view('products.index');
}
}
然后在我的web.php
中,我使用这个代码创建了一条路线
Route::get('/boutique', 'ProductController@index');
但是它不起作用。
首先,当我使用Laragon转到我在localhost上为我的项目设置的漂亮url时-->Projetname.test
我得到了正常的Laravel欢迎页面,但当我尝试转到url时,我只设置了:ProjectName.test/boutique
,我得到了
"目标类[App\Http\Controllers\ProductController]不存在">
在这里阅读了自Laravel更新到V8以来的变化后,我发现更新对路由提出了一些要求,因为$namespace前缀不是自动启用的,但可以通过在RouteServiceProvider
中取消注释这一行来再次启用
// protected $namespace = 'App\Http\Controllers';
我会取消对该行的注释,然后使用php artisan route:cache
清除缓存,但它仍然不起作用。。
当我第一次开始在Laravel中研究路由问题时,我看到许多论坛发现httpd.config文件中的apache Allowoverride设置可能会导致问题,所以我将其设置从None
更改为All
,然后重新启动Laragon,但什么都不起作用。
在纠正了我控制器上的标签后,它仍然不起作用,我尝试了两种方法(旧的和新的(,它们都不适用于我,cmd不断返回我:
λ php artisan route:list
IlluminateContractsContainerBindingResolutionException
Target class [ProductController] does not exist.
at C:laragonwwwProjectNamevendorlaravelframeworksrcIlluminateContainerContainer.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
IlluminateFoundationConsoleRouteListCommand::IlluminateFoundationConsole{closure}(Object(IlluminateRoutingRoute))
2 C:laragonwwwProjectNamevendorlaravelframeworksrcIlluminateContainerContainer.php:833
ReflectionException::("Class "ProductController" does not exist")
Laravel 8路由应为
Route::get('/boutique', [NameController:class,'method']);
因此,在您的web.php文件中添加
use AppHttpControllersProductContoller
然后这样写你的路线:
Route::get('/boutique', [ProductContoller::class,'index']);
我认为在你的">ProductController";类名
我在观看了Laravel 8上关于新路由方法最常见问题的教程后发现,当取消对RouteServiceProvider的注释时,在web.php上使用旧方法也需要使用旧路由方法,所以看起来是这样的:
Route::get('/boutique', 'ProductController@index');
请使用
Route::get('/boutique', 'AppHttpControllersProductController@index');
或者使用名称路由组并指示名称空间
Route::group(['namespace' => 'AppHttpControllers'], function(){
Route::get('/boutique', 'ProductController@index');
});
让我知道它是如何工作的。