Laravel 5.3 Route::get()返回错误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');

使用以上代码后,我得到了这个错误:

Container.php第749行中的ReflectionException:Class SiteController不存在

以下是我的完整代码:

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);
    }
}

也许我只是很笨(很好(,但我从这个"希腊"Laravel文档中找不到任何对我有帮助的东西。

我希望有人以前遇到过这种情况,并能分享他们的解决方案。非常感谢。

首先运行命令composer dump-autoload

如果它不起作用,那么按照这些步骤:

步骤1:在routes'web.php内创建路由Route::get('/', 'SiteController@index');

步骤2:使用类似php artisan make:controller SiteControllercmd创建控制器

内部: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 运行服务器

希望这个帮助是你!