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

How to get the :params value?

$router->add("/api/management/themes/:params", [
            'module'     => 'management',
            'controller' => 'themes',
            'action'     => 'index',
            'params'     => 4
   ])->setName('themes-list');

How do i fetch the params value? Lets say the url is: /api/management/themes/hello/world

How do i fetch the param hello, and param world?

edited Feb '20
$this->dispatcher->getParam('someparam');

More about that: https://docs.phalcon.io/4.0/en/dispatcher

edited Feb '20

Short answer : $this->dispatcher->getParam("YOUR_PARAM")

I define it manually for more control over the URI as so:


/* file = routes.php */

$router->add("/english", [
  "namespace"  => "Api\Controllers",
  "module"     => "Api",
  'controller' => 'Language',
  'action'     => 'change',
  /* one of my params */ 'language'   => 'english'
]);
/* file = LanguageController.php */
<?php

namespace Api\Controllers;

use Phalcon\Mvc\Controller;

class LanguageController extends ControllerBase
{
  public function ChangeAction()
  {
   // get the param passed in the routes
    switch($this->dispatcher->getParam("language"))
    {
      case 'portuguese': $this->cookies->set("website_lang","pt"); break;
      case 'english':    $this->cookies->set("website_lang","en"); break;
    }

    $this->response->redirect("/");
  }
}

If the number of parameters is set and known, you can just also import them as function parameters:

function indexAction($param1,$param2){
//$param1 == 'hello'
//$param2 == 'world'
}