Replies: 2 comments
-
This package still supports php 7 laravel-permission/composer.json Line 25 in 6a3ed62 |
Beta Was this translation helpful? Give feedback.
0 replies
-
Here is how I impletemented this <?php
namespace App\Attributes;
use Attribute;
#[Attribute]
class Permission
{
public function __construct(public ?string $permission = null) {}
} <?php
namespace App\Http\Controllers;
use App\Attributes\Permission;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
public function __construct()
{
$this->addPermissions();
}
public function addPermissions(): void
{
$classReflection = new \ReflectionClass($this);
$methods = $classReflection->getMethods();
foreach ($methods as $method) {
$attributes = $method->getAttributes(Permission::class);
foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
$middlewareName = "can:{$instance->permission}";
$this->middleware($middlewareName)->only([$method->getName()]);
}
}
}
} <?php
namespace Modules\Contact\Http\Controllers;
use App\Attributes\Permission;
use App\Http\Controllers\Controller;
class ContactController extends Controller
{
#[Permission('contact.index')]
public function index()
{
// ....
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
i have a lot middleware inside my
__construct()
method on controller like thisthen i thought would it be good if each controller have each permission using php attributes?
something like this
i dont know if this already supported by laravel or not.
thank you
Beta Was this translation helpful? Give feedback.
All reactions