【问题标题】:Phalcon router sends the wrong parametersPhalcon路由器发送错误的参数
【发布时间】:2017-06-27 09:46:15
【问题描述】:

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

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

所以路由器实际上将开头的“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”

感谢您的帮助

【问题讨论】:

    标签: phalcon phalcon-routing


    【解决方案1】:

    你也需要在第一个动作参数中添加语言。那么它应该可以工作了。

    【讨论】:

      【解决方案2】:

      除了@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);
        ...
      }
      

      【讨论】:

      • 这使得一目了然地看到动作接受的参数变得更加困难。我一直使用动作参数,因为它可以帮助我解释动作 url 应该是什么样子。
      • 用少量参数听起来很合理。如果你想在控制器中有这样的信息,也许路由器注释会很方便? olddocs.phalconphp.com/en/3.0.0/reference/…
      • 那你得到了什么?有兴趣讨论,而不是争论。恕我直言,没有动作参数有什么好处?我看到的都是负面的。
      • 从来不想听起来像在争论 :) 只是在方法中与 Annotations 共享替代操作参数。 个人我不喜欢像public function blogPostAction($language, $category, $year, $month, $day, $id, $slug)这样的带有很多参数的操作,如果我们必须添加默认值怎么办?但也许你可能是对的......毕竟它并没有那么痛:)
      • 没想到你在争论,只是想确保我听起来也没有争论;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 2020-04-30
      • 2017-08-18
      • 1970-01-01
      相关资源
      最近更新 更多