I have a controller WebController at folder App\\Web\\Controllers\\WebController.
WebController
<?php namespace App\Web\Controllers;
use Illuminate\Http\Request;
class WebController extends Controller
{
public index(){
return view('layouts.master');
}
}
Route
Route::get('home_page','WebController@index');
When i call this route, the following error appears:
Class App\\Http\\Controllers\\WebController does not exist
It has to do with the routing system.
in App\\Providers\\RouteServiceProvider.php
you will find the map()
method:
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapWebRoutes();
$this->mapApiRoutes();
}
Down that class you will find the definition for the namespace of "Web Routes"
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace) // 'App\Http\Controllers'
->group(base_path('routes/web.php'));
}
Now you just need to change the namespace()
attribute to your liking
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace('App\Web\Controllers')
->group(base_path('routes/web.php'));
}
class WebController extends **Controller**
There is a problem with this relationship. You have to import controller into this file. You are missing
use App\Http\Controllers\Controller
Also, you route is wrong. Try this
Route::namespace('your namespace here')->group(function () {
Route::get('/', 'your-controller-here@your-method');
});
Do not change the configuration of the routeserviceprovider
You change default namespace in RouteServiceProvider.
protected $namespace = 'App\\Http\\Controllers';
App\\Http\\Controllers
is default namespace you can change it to protected $namespace = 'App\\Web\\Controllers';
and then your code should work
我建议使用命名空间从 Devetoka 中获得上述代码,或者仅针对 1 或两个控制器(在我的情况下),您可以简单地将完整的命名空间注入其中。这是经过测试并有效的。
Route::get('home_page','\App\Web\Controllers\WebController@index');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.