Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFR]: Replace regexp in the router with arrays for extra performance #227

Open
diplopito opened this issue Nov 28, 2021 · 0 comments
Open
Labels
new feature request Planned Feature or New Feature Request

Comments

@diplopito
Copy link

diplopito commented Nov 28, 2021

Is your feature request related to a problem? Please describe.
Current MVC routers have the problem that when there are many paths, there is a performance penalty due to the use of regular expressions.

Describe the solution you'd like
Replace the use of regular expressions with arrays

Describe alternatives you've considered
Declare a constant array with paths, a simple example:

\* PATHS contains url paths as key, controllers and actions are defined 
in a value array, i.e. ['path'] => [controller, action] */
const PATHS = [
    '/cont2/show'  => ['SecondController',  'show'],
    '/cont4/delete' => ['FourthController', 'delete'],
    // ....
];

function urlPath()
    {
        $uri = new Uri($_SERVER['REQUEST_URI']);
        $parts = explode('/', $uri->getPath());
        $controller = $parts[1] ?? "index";
        $action = $parts[2] ?? "indexAction";
        return "/$controller/$action";
        ];
    }

Then, to match and execute the routes:

try {
       $path = urlPath();
       $controller= self::PATHS[$path][0];
       $action=  self::PATHS[$path][1];

       $mvc = new $controller();
       $mvc->$action();
} catch {\Exception $e) {
       // Path not matched, throw an exception
}

The increase of performance is notorius. Furthermore, it can be extra optimized by splitting PATHS into module arrays to create microcollections, it can process variables (with regex), etc. In this way, Phalcon could handle many paths with Phalcon speed and performance!

@diplopito diplopito added the new feature request Planned Feature or New Feature Request label Nov 28, 2021
@diplopito diplopito changed the title [NFR]: [NFR]: Replace regexp in the router with arrays for extra performance Nov 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature request Planned Feature or New Feature Request
Projects
None yet
Development

No branches or pull requests

1 participant