1

I'm trying to get the project id from the url using the router. Let's say this is my URL: http://boardash.test/tasks/all/7 and I want to get the 7 in my controller.

I created a router using this:

$router->add(
    '/tasks/:action/{project}',
    [
        'controller' => 'tasks',
        ':action'    => 1
    ]
);

And try to access it using:

$this->dispatcher->getParam('project');

But when I var_dump() this, it returns null

What am I missing?

joostdelange
  • 113
  • 11

1 Answers1

0

The :action placeholder is not correct. Try like this:

$router->add(
    '/tasks/:action/{project}',
    [
        'controller' => 'tasks',
        'action'    => 1 // <-- Look here
    ]
);

UPDATE: after few tests it seems that it is a bug in the mixed array/short syntax when the named parameter is at the end of the route.

This works as intended and returns correct parameters.

// Test url: /misc/4444444/view
$router->add('/misc/{project}/:action', ['controller' => 'misc', 'action' => 2])

However this does not return correct value for {project}. It returns "view" instead of "4444444".

// Test url: /misc/view/4444444
$router->add('/misc/:action/{project}', ['controller' => 'misc', 'action' => 1])

Syntax explained in the docs: https://docs.phalconphp.com/en/3.2/routing#defining-mixed-parameters

I will investigate a little further later, but you may consider submitting an issue on github meanwhile.


Temporary solution: meanwhile you can use this workaround if it's urgent.

$router->add('/:controller/:action/:params', ['controller' => 1, 'action' => 2, 'params' => 3])

// Test url: misc/view/test-1/test-2/test-3
$this->dispatcher->getParams() // array of all
$this->dispatcher->getParam(0) // test-1
$this->dispatcher->getParam(1) // test-2
$this->dispatcher->getParam(3) // test-3
Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
  • 1
    Thanks a lot for testing it your own. Currently, I use your temporary solution which works great. Will create an issue on github! – joostdelange Jan 04 '18 at 21:40