This is my first time using Laravel's Facades and Service Providers (I'm on Laravel 5).
I have this ServiceProvider
:
<?php namespace App\Providers;
...
class AccessServiceProvider extends ServiceProvider {
public function register() {
$this->app->alias('access', 'App\Services\Permissions\Permissions');
$this->app->bindShared('access', function() {
return new Permissions();
});
return new App\Services\Permissions\Permissions();
}
public function provides(){
return ['App\Services\Permissions\Permissions'];
}
}
My Permissions class looks like this:
<?php namespace App\Services\Permissions;
class Permissions{
private $permissions;
public function __construct(){
$this->permissions = [
"AssignQuotes" => new CanAssignQuotesPermission()
];
}
public function hasPermission($permission, $user){
return $this->permissions[$permission]->canDo($user->role_id);
}
}
I added this to my aliases
in app.php
:
'Access' => 'App\Services\Permissions\Access'
I added this to my providers
in app.php:
'App\Providers\AccessServiceProvider',
I created this Facade
class:
<?php namespace App\Services\Permissions;
use Illuminate\Support\Facades\Facade;
class Access extends Facade {
protected static function getFacadeAccessor() { return 'access'; }
}
From my understanding, I register an alias, which points to a Facade class which references a binding which points to a class.
I registered the binding in the ServiceProvider which I registered in the app.php.
Now in my routes file I am testing with the following code:
Route::get('test', function(){
$user = User::find(1);
$access = Access::hasPermission("AssignQuotes", $user);
return "test";
}
However I am getting this error:
FatalErrorException in Facade.php line 213:
Call to undefined method App\Services\Permissions\Access::hasPermission()
get_class
in Facade.php line 213 to see what kind of object you have there. – bernieget_class
I get:App\Services\Permissions\Access
. Should I be getting the other class or the facade class? – ajon$instance
should definitely.be an instance ofApp\Services\Permissions\Permissions
since that's the class with thehasPermission
method. The__callStatic
intercepts calls to non-existent static methods on the Facade and transfers it to the underlying instance. – bernie