Middleware

Classes to create your logic to access to your Web App or API. It´s a layer between the controllers and views.

Go to your terminal/cmd/powershell/gitBash or other console tool and type:

$ php BoosterCLI: make:middleware Users

Then BoosterCLI makes a file in the next path:

app/middleware/

You will see next code:

<?php
use Libs\BoosterORM\BoosterORM;
class UsersMiddleware
{
    public static function firstMethod($id)
    {

    }
}
?>

Then you could create your own logic to evaluate certain condition, many developers evaluate things like, user role, user login, permissions, status, for say something.

ex: changing name of method to evalRole

<?php
use Libs\BoosterORM\BoosterORM;
use App\Models\User;
class UsersMiddleware
{
    public static function evalRole($id)
    {
        $user = User::find(1);
        if ($user) {
            if ($user->role=='admin') {
                redirect('admin/dashboard')->with('success', 'User Logged in!');
            }
        }
    }
}
?>
//EXPLANATION
//--------------------------------------------------
//then eval if $user is true or exist
//and eval if user object property role is same as 'admin' string
//if user role match use helper redirect() to redirect to the 
//url as param string
//redirect helper no need baseurl or your project
//just add the route as you added in routes/web.php routes file

Look at line 2 and 3, there are imported the BoosterORM class to handle operations in database without create other file or create connections to database in many files.

And import User model class to access to all it´s properties like;

User->id

User->name

ect....

To call it in controllers you could go to your UsersController and in method that you prefer (we are going to use index to continue the example).

<?php
use Libs\BoosterORM\BoosterORM;
class UsersController
{
    public function index()
    {
        $user = User::find(1);
        UsersMiddleware::evalRole($user);
        return view('modules.users.index');
    }
$user = User::find(1);
//var user get the value of: User class.
//User class is the user model that we create early
//User class call static method find
//find could have two params value and column to search in
//the model user table, table is a static param in class User
//in model/User.php path -just reminder
//but could send just one param when we want to search
//directly in the fild "id"

//THATS IS WHY IS SO IMPORTANT TO MAINTAIN CONVENTIONS
//-------------------------------------------------------
//to take advantage of this feature all tables must have
//id as the name of the column that is the first and
//primmary key

//call middleware
//---------------------------------
UsersMiddleware::evalRole($user);
//Call class UsersMiddleware with method that we create
//then pass the var user as param to eval the user role
//if it matches then user will be redirect to the admin dashboard

You could add the call to your Middelware classes in some methods if you want to eval just one some methods but if you want to eval and restrict access to every method ( to imagine it simple to restrict access to your views either).

then you could add a construct to your controller and call it there like this:

public function __construct()
{
    $user = User::find(1);
    UsersMiddleware::evalRole($user);
}

So you could call the logic eval just one and works with every method, in some cases is better to add middlewares one by one because not all classes eval the same or have the same permissions or some method haver more than one condition to eval.

So as simple as add two lines of code you could eval all points of access to your class UsersController that return your views :) .

Last updated