bramus / router

A lightweight and simple object oriented PHP Router

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I setup this middleware?

CodeUmair opened this issue · comments

Hello all, I am trying to achieve this, but this is something not possible correct me if I am doing something wrong here.
I have routes like this.
1: domain.com/admin
2: domain.com/admin/login
3: domain.com/admin/.* < all other pages of admin panel.

This is what I am doing right now.
//Check if admin is already logged in, redirect to admin dashboard/admin panel home page.
$router->before('GET|POST', '/admin/login', function () {
if (Session::is_Admin() === true) {
header('location: /admin');
exit();
}
});
//Check if admin is NOT logged in, redirect to admin Login Page which is "domain.com/admin/login".
$router->before('GET|POST', '/admin', function () {
if (Session::is_Admin() === false) {
header('location: /admin/login');
exit();
}
});

Till here it's working fine, But all other pages under the admin panel are not working, this is what I am using.
$router->before('GET|POST', '/admin/.*', function () {
if (Session::is_Admin() === false) {
header('location: /admin/login');
exit();
}
});

I don't want to create middleware for every single admin panel page.
Is there any way I can achieve this?
Thank you all.

Thank you all, Looks Like I found the solution.

$router->before('GET|POST', '/admin/(?!login)(.*)', function () { if (Session::is_Admin() === false) { header('location: /admin/login'); exit(); } });