Laravel : Class controller does not exist

 ̄綄美尐妖づ 提交于 2020-04-12 19:56:12

问题


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.

<?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

Error :

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


回答1:


Add Front part to:

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.

From the docs:

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




回答2:


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 Controller Class as BaseController. so you should extends BaseController not Controller:

class PlanController extends BaseController {

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


来源:https://stackoverflow.com/questions/48617556/laravel-class-controller-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!