中间件中未找到 Laravel 5 类错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30050626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 Class not found error in middleware
提问by xenish
I have created a middleware in laravel 5 called IpHitsCounter which uses a model called DeviceInfo which is inside App\Models\FrontEnd
我在 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]);
}
My Code for Model is:
我的模型代码是:
<?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'];
}
When doing so I am getting the following error: Class 'App\Models\FrontEnd\DeviceInfo' not found
这样做时,我收到以下错误: Class 'App\Models\FrontEnd\DeviceInfo' not found
although the class exists still I am getting the error.
尽管该类仍然存在,但我仍然收到错误消息。
回答by Saiyan Prince
I guess you forgot to register the middleware
.
我猜你忘了注册middleware
.
Open the file app/Http/Kernel.php
打开文件 app/Http/Kernel.php
Look for the $routeMiddleware
property.
寻找$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
];
UPDATE 1: 28th August, 2016
更新 1:2016 年 8 月 28 日
Since Laravel 5.3.* is released, there are some configurations in the routes file. Before 5.3, there was only 1 file called routes.php
, but now, there are 2 files, web.php
and api.php
and both of these files are listed at the root directory of projects inside routes
folder. Feel free to check it out.
由于 Laravel 5.3.* 发布,routes 文件中有一些配置。5.3之前,只有1名为文件routes.php
,但现在,有2个文件,web.php
并api.php
与这两个文件在内部项目的根目录中routes
的文件夹。随意检查一下。
Coming to the solution, you need to open app/Http/Kernel.php
and edit the $middlewareGroups
having the key of web
. So, it should look somewhat like this:
来到解决方案,您需要打开app/Http/Kernel.php
并编辑$middlewareGroups
具有web
. 所以,它应该看起来像这样:
'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
],