Laravel:类控制器不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48617556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:20:35  来源:igfitidea点击:

Laravel : Class controller does not exist

laravellaravel-5.5

提问by Javed

I have created a simple controller and define a function. But when i run this it returns an error that controller does not exist.

我创建了一个简单的控制器并定义了一个函数。但是当我运行它时,它返回一个错误,即控制器不存在。

In my web.php assign a route.

在我的 web.php 中分配一个路由。

<?php
  Route::get('/', function () { return view('front.welcome'); });
  Route::get('plan','PlanController@PlanActivity')->name('plan');

On otherside in my controller my code:

在我的控制器的另一边我的代码:

<?php
 namespace App\Http\Controllers\Front;
 use App\Http\Controllers\Controller as BaseController;
 use Illuminate\Http\Request;

class PlanController extends Controller {

public function PlanActivity()
{
    dd("hello");
    //return view('admin.index');
}
}

This controller created on App\Http\Controllers\Front - on front folder

这个控制器在 App\Http\Controllers\Front 上创建 - 在前面的文件夹上

Error :

错误 :

ReflectionException (-1) Class App\Http\Controllers\PlanController does not exist

ReflectionException (-1) 类 App\Http\Controllers\PlanController 不存在

回答by Alexey Mezenin

Add Frontpart to:

添加Front部分到:

Route::get('plan', 'Front\PlanController@PlanActivity')->name('plan');

Also, change the top of the controller to:

此外,将控制器的顶部更改为:

namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

And run composer du.

并运行composer du

From the docs:

文档

By default, the RouteServiceProviderincludes your route files within a namespace group, allowing you to register controller routes without specifying the full App\Http\Controllersnamespace prefix. So, you only need to specify the portion of the namespace that comes after the base App\Http\Controllersnamespace.

默认情况下,将RouteServiceProvider您的路由文件包含在命名空间组中,允许您在不指定完整App\Http\Controllers命名空间前缀的情况下注册控制器路由。因此,您只需要指定位于基本App\Http\Controllers名称空间之后的名称空间部分。

回答by Usama

First when defining route, make sure to use the correct path for the controller. the correct is:

首先在定义路由时,确保为控制器使用正确的路径。正确的是:

Route::get('plan','Front/PlanController@PlanActivity')->name('plan');

Second you have imported ControllerClass as BaseController. so you should extends BaseControllernot Controller:

其次,您已将ControllerClass导入为BaseController. 所以你应该伸出 BaseController不是Controller

class PlanController extends BaseController {

public function PlanActivity()
{
    dd("hello");
    //return view('admin.index');
}
}