【问题标题】:Cakephp Routing to generate URL parameterCakephp路由生成URL参数
【发布时间】:2015-07-13 14:11:05
【问题描述】:

Cakephp 2.0 的简单问题。

我想设置这样的路由规则:

www.abc.com/z/abc123

将解析为(包括 URL 参数)的完整 URL

www.abc.com/bookings/bookingref/?ref=abc123

bookings 是 Controller,bookingref 是 action。

有人可以教我在 routes.php 中需要写什么吗?

凯文

【问题讨论】:

    标签: cakephp cakephp-2.0


    【解决方案1】:

    在 routes.php 中:

    Router::connect('/bookingref/', array('controller' => 'bookings', 'action' => 'bookingref'));
    

    在控制器中:

    public function bookingref(){
    
    
    }
    

    所以你应该在你的函数之后有一个视图名称。即bookingref.ctp

    【讨论】:

    • 我不认为这是答案,我想将“www.abc.com/z/abc123”转换为“www.abc.com/bookings/bookingref/?ref=abc123”
    • 您需要重新表述您的问题
    • 但您也可以进行重定向。如果我在你的控制器函数return $this->redirect('/bookings/bookingref/?ref=abc123')中转到www.abc.com/z/abc123
    • 谢谢你,但我想在 routes.php 文件中做。我不想为此创建一个控制器……有什么解决方案吗?
    【解决方案2】:

    这就是我将如何实施您的解决方案:

    在 Config/routes.php 中添加:

    Router::connect('/z/:reference',
        ['controller' => 'bookings', 'action' => 'bookingref'],
        [
            'pass' => ['reference'],// Passed to corresponding function argument (order matters if 2 or more)
            'reference' => '[a-z0-9]+'// RegExp validation if you need it
        ]
     );
    

    在您的 BookingsController 中使用:

    public function bookingref($reference = null)
    {
    ...
    }
    

    【讨论】:

      【解决方案3】:

      很遗憾,Router::redirect() 无法重定向到包含变量的基于字符串的 URL。 Progredi 提到的基于控制器的方法是您最好的选择。

      【讨论】:

        猜你喜欢
        • 2015-11-20
        • 2015-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多