1

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

Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
AlesSvetina
  • 403
  • 3
  • 6
  • 19

2 Answers2

2

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

Juri
  • 1,369
  • 10
  • 16
1

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);
  ...
}
Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
  • 2
    This makes it more difficult to immediately see at a glance what parameters the action accepts. I use action parameters all the time as it helps me interpret what the action url should look like. – Quasipickle Jul 07 '17 at 15:23
  • 1
    Sounds reasonable with low amount of parameters. If you like to have such info in controller, maybe Router Annotations can be handy? https://olddocs.phalconphp.com/en/3.0.0/reference/routing.html#annotations-router – Nikolay Mihaylov Jul 07 '17 at 19:24
  • What are you gaining then? Interested in discussion, not an argument. With all due respect, what gain is there to not having action parameters? All I'm seeing is negatives. – Quasipickle Jul 07 '17 at 22:33
  • Never ever wanted to sound like arguing :) Just shared alternative with Annotations to action parameters in the method. *Personally* I don't like having actions with many parameters like `public function blogPostAction($language, $category, $year, $month, $day, $id, $slug)`, what if we have to add default values? But maybe you could be right... it doesn't hurt that much after all :) – Nikolay Mihaylov Jul 08 '17 at 08:56
  • 1
    Didn't think you were argumentative, just wanted to ensure I didn't sound argumentative too ;) – Quasipickle Jul 08 '17 at 17:12