erhsaikhasif's avatar

How to define constants in LUMEN

Hello, I am new in Lumen and I want to define some constant in somewhere in file and use this constant in controller file. Please help me. As for example USER_ROLE=5, so USER_ROLE should be accessible in any controller file. please help me. Thank You.

0 likes
2 replies
jlrdw's avatar

see define in the php manual.

davorminchorov's avatar
Level 53

Create a class (maybe a model) and put the roles there.

Example:

Role.php

class Role 
{
    const ADMIN = 1;
    const MEMBER = 2;
    const SUBSCRIBER = 3;

    public function someMethod(){}

}

You can use these constants in your controller like so:

class SomeController extends Controller 
{
    public function someMethod() 
    {
    // some example query to demonstrate how to use the constants from the Role class.
         $admins = User::roles()->where('role', Role::ADMIN)->get();
    }
}

Don't forget to import the Role class at the top of the controller file with the use statement.

1 like

Please or to participate in this conversation.