Laravel 5.3 Route::get() returns 错误 Class [classname] 不存在
Laravel 5.3 Route::get() returns error Class [classname] does not exist
我是 Laravel 的新手,这是我第一次创建控制器。我也为类似问题搜索了几个小时,但找不到任何适合我的解决方案。
当我还没有使用控制器时,我可以使用 app/Providers/RouteServiceProvider 中的这段代码显示页面。php:
Route::get('/', function(){
if(View::exists('pages.index'))
return view('pages.index');
else
return view('errors.404',['xp'=>'pages/index']);
});
当我创建一个控制器并将上面的代码块替换成这个时,问题开始了:
Route::get('/', 'SiteController@index');
使用上面的代码后,我得到了这个错误:
ReflectionException in Container.php line 749: Class SiteController does not exist
这是我的完整代码:
里面app/Providers/RouteServiceProvider.php:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\View;
class RouteServiceProvider extends ServiceProvider{
protected $namespace = 'App\Http\Controllers';
public function boot(){
parent::boot();
}
public function map(){
$this->mapApiRoutes();
$this->mapWebRoutes();
}
protected function mapWebRoutes(){
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
/*site view*/
Route::get('/', 'SiteController@index');
}
protected function mapApiRoutes(){
Route::group([
'middleware' => 'api',
'namespace' => $this->namespace,
'prefix' => 'api',
], function ($router) {
require base_path('routes/api.php');
});
}
}
里面app/Http/Controllers/SiteController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class SiteController extends Controller{
public function index(){
$this->check_page(['pages.index']);
}
public function check_page($page){
$xp = str_replace('.','/',$page);
if(View::exists($page))
return view($page);
else
return view('errors.404',$xp);
}
}
也许我只是个笨蛋(好吧),但我无法从这个 "Greek" Laravel Documentation 中找到任何对我有帮助的东西。
我希望有人以前遇到过这个问题并可以分享他们的解决方案。非常感谢。
首先运行命令composer dump-autoload
如果它不起作用,请按照以下步骤操作:
第 1 步:在 routes\web.php
内创建路线
Route::get('/', 'SiteController@index');
第 2 步:使用 cmd
创建控制器,例如 php artisan make:controller SiteController
里面:app/Providers/RouteServiceProvider.php应该是这样的:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapWebRoutes();
$this->mapApiRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::group([
'middleware' => ['api', 'auth:api'],
'namespace' => $this->namespace,
'prefix' => 'api',
], function ($router) {
require base_path('routes/api.php');
});
}
}
内部:app/Http/Controllers/SiteController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class SiteController extends Controller
{
public function index() {
return view('pages.index');
}
}
在里面创建视图页面:
resources/views/pages/index.blade.php
运行 服务器使用 cmd php artisan serve
希望对您有所帮助!
我是 Laravel 的新手,这是我第一次创建控制器。我也为类似问题搜索了几个小时,但找不到任何适合我的解决方案。
当我还没有使用控制器时,我可以使用 app/Providers/RouteServiceProvider 中的这段代码显示页面。php:
Route::get('/', function(){
if(View::exists('pages.index'))
return view('pages.index');
else
return view('errors.404',['xp'=>'pages/index']);
});
当我创建一个控制器并将上面的代码块替换成这个时,问题开始了:
Route::get('/', 'SiteController@index');
使用上面的代码后,我得到了这个错误:
ReflectionException in Container.php line 749: Class SiteController does not exist
这是我的完整代码:
里面app/Providers/RouteServiceProvider.php:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\View;
class RouteServiceProvider extends ServiceProvider{
protected $namespace = 'App\Http\Controllers';
public function boot(){
parent::boot();
}
public function map(){
$this->mapApiRoutes();
$this->mapWebRoutes();
}
protected function mapWebRoutes(){
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
/*site view*/
Route::get('/', 'SiteController@index');
}
protected function mapApiRoutes(){
Route::group([
'middleware' => 'api',
'namespace' => $this->namespace,
'prefix' => 'api',
], function ($router) {
require base_path('routes/api.php');
});
}
}
里面app/Http/Controllers/SiteController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class SiteController extends Controller{
public function index(){
$this->check_page(['pages.index']);
}
public function check_page($page){
$xp = str_replace('.','/',$page);
if(View::exists($page))
return view($page);
else
return view('errors.404',$xp);
}
}
也许我只是个笨蛋(好吧),但我无法从这个 "Greek" Laravel Documentation 中找到任何对我有帮助的东西。
我希望有人以前遇到过这个问题并可以分享他们的解决方案。非常感谢。
首先运行命令composer dump-autoload
如果它不起作用,请按照以下步骤操作:
第 1 步:在 routes\web.php
内创建路线
Route::get('/', 'SiteController@index');
第 2 步:使用 cmd
创建控制器,例如 php artisan make:controller SiteController
里面:app/Providers/RouteServiceProvider.php应该是这样的:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapWebRoutes();
$this->mapApiRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::group([
'middleware' => ['api', 'auth:api'],
'namespace' => $this->namespace,
'prefix' => 'api',
], function ($router) {
require base_path('routes/api.php');
});
}
}
内部:app/Http/Controllers/SiteController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class SiteController extends Controller
{
public function index() {
return view('pages.index');
}
}
在里面创建视图页面:
resources/views/pages/index.blade.php
运行 服务器使用 cmd php artisan serve
希望对您有所帮助!