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

Extended the Router class #18

Merged
merged 1 commit into from
Apr 19, 2013
Merged

Extended the Router class #18

merged 1 commit into from
Apr 19, 2013

Conversation

nilportugues
Copy link
Contributor

Explanation

Extended the Router class adds new functionalities while keeping the global performance impact (very) low. Modified files to allow this new feature: Router.php, Controller.php Bootstrap.php and CLBootstrap.php.

This new method uses half compiled URLs. Replacement of the regex is done wisely using str_replace avoiding innecessary loops that would add up time.

For all loops, do_while is being used to shave off some time. (Note: I offer myself to rewrite most loops to do whiles to gain performance.)

It keeps 100% compatibility with the current URL set up, translating URLs and using controllers as domains and even using translations in domain set up. Putting it simple, it is backwards compatible.

Example

Usage of the new routing system in router.config.php

//...
$config['__NO_ROUTE_FOUND__'] = 'static/index';
$config['__HOME__'] = 'home/index';

//Old style, still works :)
$config['hello_world'] = 'home/index';

//New way
$config['admin']=array
(
    'controller' => 'admin/index', //default Controller for /admin
    'pattern' => array
    (
        //New controllers under the admin route
        // <controller_path>::<controller_method> => 'url pattern'
        //If no <controller_method> is supplied, build is used
        'admin/blog/index'         => 'blog',
        'admin/blog/post::create'  => 'blog/post/create',
        'admin/blog/post::update'  => 'blog/post/{id}/update',
        'admin/blog/post::delete'  => 'blog/post/{id}/delete',

        //USER ROUTING
        'admin/users/index'         => 'users',
        'admin/users/index::create' => 'users/create',
        'admin/users/index::update' => 'users/{uid}/update',
        'admin/users/index::delete' => 'users/{uid}/delete',

        //As many more as you like
    ),
    //Placeholder array keys are defined in router_regex.config.php
    'placeholders' => array
    (
        'isInteger' => array('{uid}','{id}')
    ),
    'request_method' => array
    (
        //Key value must match the one in the pattern.
        'admin/users/index::create' => array('GET','POST')
    )
);

Required new config file: router_regex.config.php allowing different regex. Here's the default set I've come up with.

$config['isInteger']        = '(([1-9]\d?))';
$config['isHex']            = '(0[xX][0-9a-fA-F])';
$config['isFloat']          = '([-+]?\b[0-9]+(\.[0-9]+)?\b)';
$config['isBoolean']        = '^([0|1|true|false])';
$config['isAlphaNumeric']   = '([a-zA-z0-9_\-])';
$config['isString']         = '([\s\S])';
$config['isLocale']         = '([a-zA-Z][a-zA-Z])';             // eg: en
$config['isLongLocale']     = '([a-z][a-z]_[A-Z][A-Z])';        // eg: en_US

alombarte added a commit that referenced this pull request Apr 19, 2013
Extended the Router class
@alombarte alombarte merged commit 1caa6e5 into sifophp:devel Apr 19, 2013
alombarte added a commit that referenced this pull request May 22, 2013
This reverts commit 1caa6e5, reversing
changes made to 1443824.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants