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

Help ! I want to get the uri segment in the volt.

The url is like this : https://www.abc.com/user/profile/5

how could i get the '5'?

i looked up something about this in the documentation, i can't get the answer in the end.

that's all , my english is poor. thank you!!



43.9k

Hi,

are you using a micro app or a full stack mvc ?

"5" will be passed to your action as the first argument. Your controller action needs to set that value in the view, as a variable (for example, user_id). You can then access "5" in Volt by accessing user_id. For example, {{ user_id }}.



10.5k
Accepted
answer

There are many ways to access URL parameters in view but the best practice is that you should get it from dispatcher and pass it to view via controller/action

a basic example:

// URL : https://www.abc.com/user/profile/5

// in action
$this->view->user_id = $this->dispatcher->getParams()[0];
// in view
{{ user_id }}

Or directly in volt file

{% var params = dispatcher.getParams() %}
{{ params[0] }}

i solved my question. Thanks all!

thank you very much

"5" will be passed to your action as the first argument. Your controller action needs to set that value in the view, as a variable (for example, user_id). You can then access "5" in Volt by accessing user_id. For example, {{ user_id }}.

i just write this for pratising,bro.

Hi,

are you using a micro app or a full stack mvc ?