Skip to content

RFC: the Dispatcher rule system #525

Open
@jails

Description

@jails

Take, for example, the following code :

//Rule 1 : camelize action
MockDispatcher::config(array('rules' => array(
    'action' => array('action' => function($params) {
        return Inflector::camelize(strtolower($params['action']), false);
    })
)));
//Rule 2 : prefix action
MockDispatcher::config(array('rules' => array(
    'admin' => array('action' => 'admin_{:action}')
)));

Router::connect('/', array('controller' => 'test', 'action' => 'my_test', 'admin' => true));
MockDispatcher::run(new Request(array('url' => '/')));
$result = end(MockDispatcher::$dispatched);

echo $result->params['action'];

Since the default route is an 'admin' route the expected value could be : "admin_my_test", "admin_myTest" or "adminMyTest" since this route match the two rules ('action' & 'admin'). But the result is "myTest". However I think it's more than a kind of "bug".

Indeed, the whole Dispatcher::applyRules() need to be refactored for the following reasons imo:

  • The order of rules are important : with a single 'admin' rule conflicts are rare, but an 'action' rule will be applied almost all the time.
  • The first rule inserted is the last rule applied
  • The last rule applied imposes its transformation to all previous transformations : with the previous point, if 'action' is the first rule inserted using Dispatcher::config(), it will override tranformations of all other rules.
  • Once Dispatcher::$_rules is configured as a callback you can't add no more rules
  • The rules involved to the Dispatcher : this mean if you configure your own rule 'action' all plugins must now respect the convention you set.

With a Dispatcher::reset() and having all the previous points in mind workaround can be putted in place. But if Dispatcher::applyRules() is intended to be used also by plugins, the way Dispatcher::applyRules() works still an issue imo.

Imo rules should be removed from the Dispatcher since each plugin should manage their own rules.

Imo this issue is in a way related to #416 since it shows a possible syntax for setting a specific configuration to a bunch of routes (whether related to the the way it must be parsed, matched etc.)

So for me the Dispatcher::applyRules() feature concern more the Router class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions