Zizaco / entrust

Role-based Permissions for Laravel 5
MIT License
6.05k stars 1.29k forks source link

App\Role not found Laravel 5.2 #498

Open Silvio8 opened 8 years ago

Silvio8 commented 8 years ago

I'm trieng to use Entrust in Laravel 5.2. I modified it a bit that's work.

So i can create Groups and permissions. But if i try to use it in a blade or controller it doesn't work anymore: if(Auth::user()->hasRole(['destroy-kostentraeger']))

i get following error: ErrorException in Model.php line 1011: Fatal error: Class 'App\Role' not found (View: G:...\htdocs\test1\resources\views\home.blade.php)

Please help

mickeyschwab commented 8 years ago

Did you create your own App\Role model then use the EntrustRoleTrait?

Silvio8 commented 8 years ago

I created the Role and Permission Model in App\Models\ And changed all path also to App\Models\Roles. But it was still not working? It seems somewhere is codeded that this file can be find under App\Role. But a replaced this string in the whole project. Now i changed the path of this two models direktly to App and it works.

Cannonb4ll commented 8 years ago

Post the contents of your permission and role model please, so we can review your models.

Silvio8 commented 8 years ago

But the mistake is not in the Model file. It's somewhere in a config file where there are using App\Role for example. But here are my Models if i store them in App\Models

namespace App\Models;

use Zizaco\Entrust\EntrustPermission;

class Permission extends EntrustPermission { }

namespace App\Models;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole { }

Cannonb4ll commented 8 years ago

Ah, I see.

Open up config/entrust.php

Set role setting to:

'role' => Role::class,

And permission setting to:

'permission' => Permission::class,

Then at the top of your entrust config file add:

use App\Models\Permission;
use App\Models\Role;

Then you should be good to go :)

(Be sure to publish the config file first, otherwise you won't find entrust.php in the config folder)

Cannonb4ll commented 8 years ago

Can you confirm this fixed the issue @Silvio8 ?

If so then you can close the issue :)

blancomaberino commented 8 years ago

Hello, I am having the same issue. The problem seems to be that, for any reason, the file "entrust.php" is not being read and /vendor/zizaco/entrust/src/config/config.php is being read instead. Any clue about why is this happening?

blancomaberino commented 8 years ago

I finally found the issue. Configuration was being cached /bootstrap/cache/config.php and it was using namespace defined there. Just deleted it and it is working fine now. @Silvio8 does it work for you?

Silvio8 commented 8 years ago

I didn't tried it jet. I'm using it in the datandard folder like App\Role.php

yuanhuanglin commented 8 years ago

I had the same problem for Laravel 5.2. I tried add these three lines

        \Zizaco\Entrust\Middleware\EntrustRole::class,
        \Zizaco\Entrust\Middleware\EntrustPermission::class,
        \Zizaco\Entrust\Middleware\EntrustAbility::class,

into $middleware array in Kernel.php, the App\Role error message disappeared, but I still got the error message like this.

ErrorException in EntrustRole.php line 36:Missing argument 3 for Zizaco\Entrust\Middleware\EntrustRole::handle()

Here is my routing...

Route::group(['middleware' => 'web'], function () {

    Route::group(['middleware' => 'auth'], function () {

        Route::get('/', function () {
            return view('home');
        });

        Route::group(['middleware' => ['role:Admins']], function() {
            Route::resource('supplierprices', 'SupplierPricesController');
        });
    });
});
dlopez commented 8 years ago

I just installed the latest version of entrust from the dev-laravel-5 branch. Followed all the instructions currently on the github page. The only change to the instructions was changing the location of the models from App to App\Models. I also changed this on the entrust config generated from php artisan vendor:publish.

When trying to attach a permission.

    $user = App\User::first();

    $admin = new App\Models\Role;
    $admin->name         = 'admin';
    $admin->display_name = 'User Administrator'; // optional
    $admin->description  = 'User is allowed to manage and edit other users'; // optional
    $admin->save();

    $user->attachRole($admin);

I got the following error.

FatalThrowableError in Model.php line 956:
Class 'App\Role' not found

My User Model;

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Authenticatable
{
    use EntrustUserTrait;
mayne commented 7 years ago

If you put Role model in App\Models, the first step you should do is set the namespace in Role model as "namespace App\Models"

atishrajput commented 7 years ago

edit your Role.php file & add namespace APP;

edit your Permission.php file & add namespace App;