Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Class not found error in middleware

enter image description hereI have created a middleware in laravel 5 called IpHitsCounter which uses a model called DeviceInfo which is inside 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

although the class exists still I am getting the error.

like image 567
xenish Avatar asked May 05 '15 10:05

xenish


1 Answers

I guess you forgot to register the middleware.

Open the file app/Http/Kernel.php

Look for the $routeMiddleware property.

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

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.

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:

'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
],
like image 180
Saiyan Prince Avatar answered Sep 17 '22 13:09

Saiyan Prince