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

Router parameters default value equals to position #10428

Closed
Surt opened this issue May 28, 2015 · 2 comments
Closed

Router parameters default value equals to position #10428

Surt opened this issue May 28, 2015 · 2 comments

Comments

@Surt
Copy link
Contributor

Surt commented May 28, 2015

Using named parameters on the standard router I get a default value equal to the position, when it's not defined:

    $router->addGet("/api/:module/([a-zA-Z0-9_-]+)(/json|/csv)*", array(
        'module' => 1,
        'controller' => 'index',
        'action' => 'view',
        'element' => 2,
        'type' => 3,
        'api' => true
    ));

using, on the controller:

$dispatcher->getParam('type')
api/user/1 ==> 3 // I don't want the 3 to be shown, the parameter is not set

api/user/1/json ==> '/json'

The problem comes when I try to parse the "type" parameter, I must check it vs the value 3, but in other routes I will have it as the second parameter so....

How can I accomplish this? Maybe there is another way to do it?

Also, notice that the "api" value is setted in a strange way:

api => user

I did a discussion post some time ago:
http://forum.phalconphp.com/discussion/6674/router-default-for-named-parameters-at-the-end-of-the-route#C18867

@Surt
Copy link
Contributor Author

Surt commented May 28, 2015

Test for this:

$testRoutes = array(
            '/api/user/rer',
            '/api/user',
            '/api/user/rer/json'
        );

        $router = new \Phalcon\Mvc\Router(false);

        $router->add("/api/:module/([a-zA-Z0-9_-]+)(/json|/csv)*", array(
            'module' => 1,
            'controller' => 'index',
            'action' => 'view',
            'element' => 2,
            'type' => 3,
            'api' => true
        ));
        foreach ($testRoutes as $testRoute) {

            //Handle the route
            $router->handle($testRoute);

            echo 'Testing ', $testRoute, '<br>';

            //Check if some route was matched
            if ($router->wasMatched()) {
                echo 'Module: ', $router->getModuleName(), '<br>';
                echo 'Controller: ', $router->getControllerName(), '<br>';
                echo 'Action: ', $router->getActionName(), '<br>';
                echo '<pre>Params: ', print_r($router->getParams(), true), '</pre><br>';
            } else {
                echo 'The route wasn\'t matched by any route<br>';
            }
            echo '<br>';
        }

@andresgutierrez
Copy link
Contributor

Fixed in 2.1.x branch

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

No branches or pull requests

2 participants