Search code examples
phplaravelclassmiddlewaredashboard

Laravel 5.3: Class company does not exist


I'm building a business directory where is going to have 2 different dashboards accordingly to the roles of the users, which can be: Admin or Company.

In order to handle this, the following procedure was developed:

1- Create a new middleware:

php artisan make:middleware Company

2- Code for the Company middleware (which is the same for admin and comunity):

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class Company
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check()){
            if(Auth::user()->isCompany()){
                return $next($request);     
            }
        }
        return redirect('/');
    }
}

3- Added the following function in my User model:

public function isCompany(){
    if($this->role->Role_Type  == "Company" && $this->is_active == 1){
        return true;
    }
    return false;
}

4- Handled the routing

Route::group(['middleware'=>'company'], function(){
     Route::get('/company', function(){
        return view('company.index');
    });
});

5- Created a new user with the role company

At the moment, once trying to login with that User, always get prompt with this message:

ReflectionException in Container.php line 734: Class company does not exist

Any ideas in how to solve this?

To note: already tried: composer dump-autoload


Solution

  • The first problem I see is that in the code you pasted, the class name is Community instead of Company. The class name should match the name of the file, case and all. Also, can you share your middleware file? If the naming is not the issue, there may be a reference issue inside of one of your provider or kernel files.