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

Unusual routing behavior with a named parameter

Hello!

I have the following route for an administration interface:

$this->add('/{domain}/{controller}/{action}/:params', ["params" => 4]);

It allows administration of several different domains and has clearly defined what elements are mandatory (Domain, Controller and Action) and can optionally take parameters. Here's one example of how it should work:

The following route admin.mydomain.com/editdomain.com/text/edit/2 calls the editAction in TextController and passes 2 as the parameter. The action is defined as:

public function editAction ($id = NULL) {}

If this action is provided with an ID, it tries to find a Text in the database for editing. If it doesn't get the ID it will generate a new Text object and edit that. That should happen if the following route is called: admin.mydomain.com/editdomain.com/text/edit

However, when that route is called, $id suddenly becomes "domain.com" even though params are clearly set as ["params" => 4].

I've tried editing the editAction in the following way:

public function editAction ($var1 = NULL, $var2 = NULL, $var3 = NULL, $var4 = NULL) {}

And here are the results:

admin.mydomain.com/editdomain.com/text/edit
    var 1: editdomain.com
    var 2:
    var 3:
    var 4:

admin.mydomain.com/editdomain.com/text/edit/1
    var 1: 1
    var 2: editdomain.com
    var 3:
    var 4:

admin.mydomain.com/editdomain.com/text/edit/1/test2
    var 1: 1
    var 2: test2
    var 3: editdomain.com
    var 4:

Why is the {domain} always appended to the variables forwarded to the action and can I avoid that somehow?



13.8k

What happens if you name all parts? Like :controller/:action/:params etc.



2.9k

That would be a little problematic because it makes each uri element optional and then other routes take precedence.

For now we've made a workaround. We're checking the "var 2" if it's NULL in order to know if "var 1" is the ID or just the domain.