Laravel 5 Class not found Error in middleware
I have created a middleware in laravel 5 called IpHitsCounter that uses the DeviceInfo model which is inside the application \ 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 the model:
<?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 this, I get the following error: Class 'App\Models\FrontEnd\DeviceInfo' not found
although the class exists, but I am getting the error.
source to share
I forgot to register middleware
.
Open the file app/Http/Kernel.php
Find the 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: Aug 28, 2016
Since Laravel 5.3 has been released. *, there are several configurations in the routes file. Before 5.3 there was only one file named routes.php
, but now there are 2 files, web.php
and api.php
, and both of these files are listed in the project root directory inside the folder routes
. Feel free to check it out.
Getting started with the solution, you need to open app/Http/Kernel.php
and edit $middlewareGroups
with a key web
. So it should look something like this:
'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
],
source to share