【问题标题】:What's the difference between :Args and :CaptureArgs in Catalyst?Catalyst 中的 :Args 和 :CaptureArgs 有什么区别?
【发布时间】:2012-06-22 02:16:25
【问题描述】:

我通常可以通过随机尝试这两个选项的不同排列来获得我想要的行为,但我仍然不能说我确切地知道它们的作用。有没有具体的例子来说明这种差异?

【问题讨论】:

    标签: perl catalyst


    【解决方案1】:

    :CaptureArgs(N) 匹配如果至少还有 N 个参数。它用于非终端链式处理程序。

    :Args(N) 仅在恰好剩下 N 个 args 时才匹配。

    例如,

    sub catalog : Chained : CaptureArgs(1) {
        my ( $self, $c, $arg ) = @_;
        ...
    }
    
    sub item : Chained('catalog') : Args(2) {
        my ( $self, $c, $arg1, $arg2 ) = @_;
        ...
    }
    

    匹配

    /catalog/*/item/*/*
    

    【讨论】:

      【解决方案2】:

      CaptureArgs 用于 Catalyst 的链式方法中。

      Args 标志着链式方法的结束。

      例如:

      sub base_method : Chained('/') :PathPart("account")  :CaptureArgs(0)
      {
      
      }
      sub after_base : Chained('base_method') :PathPart("org") :CaptureArgs(2)
      {
      
      }
      sub base_end : Chained('after_base') :PathPart("edit")  :Args(1)
      {
      
      }
      

      以上链接方法匹配/account/org/*/*/edit/*

      这里base_end是链的结束方法。使用Args标记链动作的结束。如果使用CaptureArgs,则表示链仍在进行中。

      Args 也用于催化剂的其他方法中,用于指定方法的参数。

      同样来自 cpan Catalyst::DispatchType::Chained:

      The endpoint of the chain specifies how many arguments it
       gets through the Args attribute. :Args(0) would be none at all,
       :Args without an integer would be unlimited. The path parts that 
      aren't endpoints are using CaptureArgs to specify how many parameters
       they expect to receive.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-03
        • 1970-01-01
        • 2012-06-17
        • 1970-01-01
        • 2020-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多