flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.61k stars 407 forks source link

Middleware code #514

Closed n0nag0n closed 6 months ago

n0nag0n commented 6 months ago

This is an attempt at middleware code

krmu commented 6 months ago

Good evening! I tried some simple idea about auths.

$auth = new class {
    public $is_logged_in = false;
    public function before() {
        if (!$this->is_logged_in) {
            if (Flight::request()->url != "/login") Flight::redirect("/login");
        }else{
            if (Flight::request()->url != "/dashboard") Flight::redirect("/dashboard");
        }
    }
};

Flight::route('/login', function () {
    echo 'login page';
},false,'login_page');
Flight::route('/dashboard', function () {
    echo 'Dashboard page';
},false,'dashboard');

Flight::route('/', function () {
    echo 'hello world!';
})->addMiddleware($auth);

Flight::start();

I'm probably missing something about getUrl, but once I tried to mix methods im getting this error:

No route found with alias: login_page (0)
#0 D:\xampp\htdocs\core\flight\Engine.php(771): flight\net\Router->getUrlByAlias('login_page', Array)
#1 D:\xampp\htdocs\core\flight\core\Dispatcher.php(225): flight\Engine->_getUrl('login_page')
#2 D:\xampp\htdocs\core\flight\core\Dispatcher.php(164): flight\core\Dispatcher::invokeMethod(Array, Array)
#3 D:\xampp\htdocs\core\flight\core\Dispatcher.php(56): flight\core\Dispatcher::execute(Array, Array)
#4 D:\xampp\htdocs\core\flight\Engine.php(116): flight\core\Dispatcher->run('getUrl', Array)
#5 D:\xampp\htdocs\core\flight\core\Dispatcher.php(225): flight\Engine->__call('getUrl', Array)
#6 D:\xampp\htdocs\core\flight\Flight.php(120): flight\core\Dispatcher::invokeMethod(Array, Array)
#7 D:\xampp\htdocs\core\index.php(8): Flight::__callStatic('getUrl', Array)
#8 D:\xampp\htdocs\core\flight\core\Dispatcher.php(221): class@anonymous->before()
#9 D:\xampp\htdocs\core\flight\core\Dispatcher.php(164): flight\core\Dispatcher::invokeMethod(Array, Array)
#10 D:\xampp\htdocs\core\flight\Engine.php(400): flight\core\Dispatcher::execute(Array, Array)
#11 D:\xampp\htdocs\core\flight\core\Dispatcher.php(221): flight\Engine->_start()
#12 D:\xampp\htdocs\core\flight\core\Dispatcher.php(164): flight\core\Dispatcher::invokeMethod(Array, Array)
#13 D:\xampp\htdocs\core\flight\core\Dispatcher.php(56): flight\core\Dispatcher::execute(Array, Array)
#14 D:\xampp\htdocs\core\flight\Engine.php(116): flight\core\Dispatcher->run('start', Array)
#15 D:\xampp\htdocs\core\flight\core\Dispatcher.php(221): flight\Engine->__call('start', Array)
#16 D:\xampp\htdocs\core\flight\Flight.php(120): flight\core\Dispatcher::invokeMethod(Array, Array)
#17 D:\xampp\htdocs\core\index.php(26): Flight::__callStatic('start', Array)
#18 {main}

With code:

$auth = new class {
    public $is_logged_in = false;
    public function before() {
        if (!$this->is_logged_in) {
            if (Flight::request()->url != Flight::getUrl('login_page')) Flight::redirect(Flight::getUrl('login_page'));
        }else{
            if (Flight::request()->url != Flight::getUrl('dashboard')) Flight::redirect(Flight::getUrl('dashboard'));
        }
    }
};

Flight::route('/login', function () {
    echo 'login page';
},false,'login_page');
Flight::route('/dashboard', function () {
    echo 'Dashboard page';
},false,'dashboard');

Flight::route('/', function () {
    echo 'hello world!';
})->addMiddleware($auth);

Flight::start();
n0nag0n commented 6 months ago

Thanks for trying it out. Looks like it's trying to work, but getting stuck in the alias for now. I still need to go through a lot of test scenarios here. For the time being, the thing that might be the most helpful is to just look over the code and implementation and see if it makes sense. You can see the couple use cases I started with in the unit tests file.

krmu commented 6 months ago

Im not sure, but last commit might fix this, since issue was with path.

516

krmu commented 6 months ago

Works when you use #516 code.