1

我在 Phalcon 中的路由器有问题。我的控制器中有一个动作,以太是否需要日期参数。因此,当我访问 URL 时:http ://example.com/sl/slots/index/2017-06-27 一切正常。但是当我去:http ://example.com/sl/slots/index 我得到以下错误:

DateTime::__construct(): 无法在位置 0 (s) 解析时间字符串 (sl): 在数据库中找不到时区。

所以路由器实际上将开头的“sl”作为参数。

我的这种 url 的路由器设置如下:

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

顺便说一句,没有索引它也一样:http ://example.com/sl/slots

哦,我的插槽索引操作如下所示:

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

因此,当我在没有参数的情况下调用操作时,$currentDate 设置为“sl”

感谢您的帮助

4

2 回答 2

2

好吧,您也需要在第一个动作参数中添加语言。然后它应该工作。

于 2017-06-27T10:07:15.937 回答
1

除了@Juri 的回答。我更喜欢让我的操作保持空白或尽可能苗条。想象一下,如果你在 Route 中有 3-4 个参数,你最终会得到类似的结果:

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

以下是我更喜欢处理 Route 参数的方式:

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);
  ...
}
于 2017-06-27T11:07:13.717 回答