【问题标题】:Laravel 5.3 Route::get() returns error Class [classname] does not existLaravel 5.3 Route::get() 返回错误类 [classname] 不存在
【发布时间】:2017-03-06 05:14:34
【问题描述】:

我是 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:类 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 Documentation 中找到任何对我有帮助的东西。

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

【问题讨论】:

  • Route::get('/', 'SiteController@index');放到routes/web.php里面看看?
  • 你在routes/web.php中为路由输入过吗?
  • 我只是这样做了,还添加了 use Illuminate\Support\Facades\View;在我的 SiteController.php 之上。但是现在我得到了一个新错误: FileViewFinder.php 第 71 行中的 ErrorException:isset 中的非法偏移类型或为空
  • 抱歉,我刚刚通过删除 $this->check_page(['pages.index']);...

标签: php model-view-controller routes laravel-5.3


【解决方案1】:

首先运行命令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 运行服务器

希望对你有帮助!

【讨论】:

  • composer dump-autoload 没有做任何事情,但是按照您的指示帮助我找出了错误的根源。在我的 SiteController->index 中,我将行 $this-&gt;check_page(['pages.index']); 更改为 return $this-&gt;check_page(['pages.index']);。缺少的return 字解决了一切。非常感谢您的指导。
  • 旁注: php artisan serve 给了我 [ErrorException] chdir(): No such file or directory (errno 2)。目前这并不是什么大不了的事,但如果你知道我为什么会得到这个,那也将不胜感激。
  • @FlameDenise 您必须在项目目录文件夹中运行命令 例如:您的项目目录是:c:\xampp\htdocs\laravel5.3laravel5.3 文件夹中运行 cmd:php artisan serve
  • 现在解决了!我之前更改了我的目录,以便从我的 URL 中删除“公共”。我在 C:\xampp\htdocs\laravel\project 中运行了该命令,它起作用了。感谢您的帮助。
猜你喜欢
  • 2021-01-09
  • 2015-12-05
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 2015-03-01
  • 2020-09-09
  • 2017-01-14
  • 1970-01-01
相关资源
最近更新 更多