Laravel 5中间件出现类未找到错误

5

enter image description here我已经在 Laravel 5 中创建了一个名为 IpHitsCounter 的中间件,它使用了一个名为 DeviceInfo 的模型,该模型位于 App\Models\FrontEnd 中。

<?php namespace App\Http\Middleware\FrontEnd;

    use Closure;
    use Request;
    use BrowserDetect;
    use App\Models\FrontEnd\DeviceInfo;
    use DB;

    class IpHitsCounter {

        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next)
        {
         DeviceInfo::create(['devices'=>$agentDevice,'deviceFamily'=>$deviceFamily]);
        }

我的模型代码是:

<?php namespace App\Models\FrontEnd;

use Illuminate\Database\Eloquent\Model;

class DeviceInfo extends Model {

   /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'client_device_infos';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['devices', 'deviceFamily'];



}

当我这样做时,我遇到了以下错误:找不到类'App\Models\FrontEnd\DeviceInfo'。尽管该类存在,但我仍然会遇到错误。

尝试过了,不起作用 @GauravDave - xenish
你的模型是否在正确的目录中?应该是 app/Models/FrontEnd/DeviceInfo.php - lukasgeiter
IpHitsCounter 文件的 31 行有什么内容? - Saiyan Prince
DeviceInfo::create(['devices'=>$agentDevice,'deviceFamily'=>$deviceFamily]); 这段代码是为了方便,我只在问题中展示了这段代码,因为出现了错误。 - xenish
我之前没有使用Artisan命令创建我的模型,后来我使用了Artisan命令重新创建了模型,一切都正常工作了。感谢您关注我的问题,@user3514160。 - xenish
显示剩余4条评论
1个回答

17

我猜你忘记注册中间件了。

打开文件app/Http/Kernel.php

查找$routeMiddleware属性。

protected $routeMiddleware = [
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
    'App\Http\Middleware\VerifyCsrfToken',
    'App\Http\Middleware\FrontEnd\IpHitsCounter' // your middleware
];

更新 1:2016年8月28日

自从发布Laravel 5.3以来,路由文件中有一些配置。在5.3之前,只有一个名为routes.php的文件,但现在有两个文件,web.phpapi.php,这两个文件都列在项目的根目录下的routes文件夹中。可以随意查看。

进入解决方案,您需要打开app/Http/Kernel.php并编辑具有键web$middlewareGroups。因此,它应该看起来像这样:

'web' => [
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\VerifyCsrfToken::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,

    \App\Http\Middleware\FrontEnd\IpHitsCounter::class // your middleware
],

我已经将其添加为protected $routeMiddleware,不这样做我认为它不会运行。中间件一切正常,它正在工作,但我的唯一问题是使用我创建的模型。 - xenish
正如@lukasgeiter的评论所述,文件名是否正确且放置在正确位置?请进行交叉检查以确认。 - Saiyan Prince
请提供“堆栈跟踪(stacktrace)”好吗? - Saiyan Prince
6
这是全局中间件数组。如果您把您的中间件放在那里,它将在每个HTTP请求时被执行。请在这里查看http://laravel.com/docs/5.1/middleware#registering-middleware。这个答案完全错误。 - Gaurav Deshpande

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接