We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Route

I have my route:

    $di->set('router', function () {
    $router = new Router(false);
    $router->setDefaultModule("backend");
    $router->setDefaultAction('index');
    $router->notFound(array(
        'module' => 'public',
        'controller' => 'error',
        'action' => 'index'
    ));

    $router->add('/', array(
        'module' => 'public',
        'controller' => 'login',
        'action' => 'index',
    ));
    $router->add('/:module/:controller', array(
        'module' => 1,
        'controller' => 2,
        'action' => 'index',
    ));
    $router->add('/:module/:controller/{param:[a-z]+}\.(html|json|xml)', array(
        'module' => 1,
        'controller' => 2,
        'action' => 3,
        'params' => 4
    ));
    $router->add('/:module/:controller/([a-z]+)\.(json|html)/:params', array(
        'module' => 1,
        'controller' => 2,
        'action' => 3,
        'type' => 4,
        'params' => 5
    ));

    return $router;
});

On controller:

 public function changeLanguageAction($lang = NULL) {
    var_dump($lang);
    die();
}

the problem is:

if the uri: https://feedback/public/kernel/changelanguage.html/es
response: string(2) "es"

if the uri: https://feedback/public/kernel/changelanguage.html/en
response: string(2) "en"

But if the uri: https://feedback/public/kernel/changelanguage.html
response: string(4) "html"

I want that if not params after the action_name.(html/json/xml) var_dump show NULL

Any Help!



642
Accepted
answer

Hello.

I'd recommend you to change this

$router->add('/:module/:controller/([a-z]+)\.(json|html)/:params', array(

to

$router->add('/:module/:controller/{param:[a-z]+}\.(json|html)/:params', array(

Also, in the controller I'd use this code:

$this->dispatcher->getParam("params", NULL, NULL);
edited May '15

Hi, @denismosolov

the url:

https://feedback/common/error/index.html/500

when i try to:

in controller:

public function initialize() {
    $this->view->setMainView('error');        
    $this->dispatcher->getParam("params");

always return NULL;

and if i try:

var_dump($this->dispatcher->getParams());

the response:

array(3) { [0]=> string(3) "500" ["type"]=> string(4) "html" ["param"]=> string(6) "common" } 

I want, to get "500" becouse this is de first params...

    $this->dispatcher->getParam("params"); // but thisnt work

My route now is:

$di->set('router', function () {
$router = new Router(false);
$router->setDefaultModule("backend");
$router->setDefaultAction('index');
$router->notFound(array(
    'module' => 'common',
    'controller' => 'error',
    'action' => 'index',
    'params' => '0404'
));

$router->add('/', array(
    'module' => 'public',
    'controller' => 'login',
    'action' => 'index',
));
$router->add('/:module/:controller', array(
    'module' => 1,
    'controller' => 2,
    'action' => 'index',
));
$router->add('/:module/:controller/{param:[a-z]+}\.(json|html)/:params', array(        
    'module' => 1,
    'controller' => 2,
    'action' => 3,
    'type' => 4,
    'params' => 5
));
$router->add('/:module/:controller/([a-z]+)\.(json|html)', array(
    'module' => 1,
    'controller' => 2,
    'action' => 3
));
return $router;
});

HElp!

Why,

        array(3) { [0]=> string(3) "500" ["type"]=> string(4) "html" ["param"]=> string(6) "common" } 

common is a param?, and not the controller?



642

Let's change this

$router->add('/:module/:controller/{param:[a-z]+}\.(json|html)/:params', array(

to

$router->add('/:module/:controller/{param:[a-z]+}\.(json|html)/{code:[0-9]+}', array(

then in the controller it should return '500'

$this->dispatcher->getParam("code");

not works :(

    $router->add('/:module/:controller/{param:[a-z]+}\.(json|html)/{code:[0-9]+}', array(
    'module' => 1,
    'controller' => 2,
    'action' => 3,
    'type' => 4,
    'params' => 5
));

in controller:

$param = $this->dispatcher->getParam('code');
    echo $param;
    die();

response: index



5.7k

With the routing, there are a few parameter names that are reserved by the framework and have special meanings. These include the following:

  • :module
  • :controller
  • :action
  • :params
  • :namespace
  • :int

From the list above: module, controller, and action are all automatically checked for alpha-numeric string only. Both module and controller allow hyphens, but action does not.

You can read more about it here: https://docs.phalcon.io/en/latest/reference/routing.html#defining-routes

If the alpha-numeric filter is not good enough and you can not use :action becuase you want lowercase alpha characters only, you should be able to rewrite the routes like this:

$router->add('/:module/:controller/([a-z]+)\.(json|html)/([a-z]{2})', array(
    'module' => 1,
    'controller' => 2,
    'action' => 3,
    'type' => 4, // html or json
    'lang' => 5 // Language (es,en,etc...) - I limited this to requiring 2 characters "{2}"
));

$router->add('/:module/:controller/([a-z]+)\.(json|html)', array(
    'module' => 1,
    'controller' => 2,
    'action' => 3,
    'type' => 4, // html or json
));

I ran these against multiple test cases and every one came out as expected. I hope it works for you as well.