简体   繁体   中英

Phalcon router sends the wrong parameters

I have a problem with my router in Phalcon. I have an action in my controller which ether takes a date parameter or not. So when I access an URL: http://example.com/sl/slots/index/2017-06-27 everything works ok. But when I go to: http://example.com/sl/slots/index I get the following error:

DateTime::__construct(): Failed to parse time string (sl) at position 0 (s): The timezone could not be found in the database.

So the router actually takes the "sl" in the beginning as a parameter.

My router for this kind of url is set like this:

$router->add(
    "/{language:[a-z]{2}}/:controller/:action",
    array(
        "controller" => 2,
        "action"     => 3
    )
);

Btw it does the same withut the index: http://example.com/sl/slots

Oh and my slots index action looks like this:

public function indexAction($currentDate = false){ //code }

So the $currentDate is set to "sl" when I call the action without a parameter

Thank you for the help

Well you need to add language in first argument of action too. Then it should work.

In addition to @Juri's answer.. I prefer to keep my Actions empty or as slim as possible. Imagine if you have 3-4 parameters in the Route, you will end up with something like:

public function indexAction($param1 = false, $param2 = false, $param3 = false....) 

Here is how I prefer to handle Route parameters:

public function indexAction()
{
  // All parameters
  print_r($this->dispatcher->getParams());

  // Accessing specific Named parameters
  $this->dispatcher->getParam('id');
  $this->dispatcher->getParam('language');

  // Accessing specific Non-named parameters
  $this->dispatcher->getParam(0);
  $this->dispatcher->getParam(1);
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM