bramus / router

A lightweight and simple object oriented PHP Router
MIT License
1.09k stars 245 forks source link

How can I setup this middleware? #206

Closed CodeUmair closed 1 year ago

CodeUmair commented 1 year ago

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.

CodeUmair commented 1 year ago

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(); } });