skipperbent / simple-php-router

Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.
647 stars 121 forks source link

Loaded route not available to middelware? #623

Open esbenboye opened 1 year ago

esbenboye commented 1 year ago

I'm using the latest version of this package (4.3.7.2)

I just tried to create a small middleware to check if a user is authorized to do a certain action. I was using SimpleRouter::request()->getLoadedRoute()->getParameters() inside the middleware to get the parameters from the route, but that failed since the value from getLoadedRoute() was null.

An easy fix would be to move Router.php line 408 ($this->request->addLoadedRoute($route);) up before the event EVENT_MATCH_ROUTE is triggered to make the loaded route available to the events. I haven't noted any negative side effects by making this change.

But the question is if this is 'by design' or if I should open a pull request?

https://user-images.githubusercontent.com/4438511/196339422-e62d4ed0-569e-4978-8b6c-b2920ba1560f.mov

DeveloperMarius commented 1 year ago

Hey,

the PR #604 sould do what you want to do. I merged it a while ago with my fork.

Warning: I changed a lot in this fork and currently I only support the php8 branch (I think php 8.1 is required). You can use it with composer:

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/developermarius/simple-php-router"
        },
        {
            "type": "composer",
            "url": "https://packagist.org"
        }
],
"require": {
  "pecee/simple-router": "dev-v5-development-php8"
}

In this year I had no time to do the README.md and stuff like this, but I hope to make some time soon to also do some changes to the input validation topic. A lot was explained in #607 (validation) and a lot of changes were made to the InputHandler. If you have any questions, use this comment section.

~ Marius