【问题标题】:CakePHP 2.x Reverse Routing Link HelperCakePHP 2.x 反向路由链接助手
【发布时间】:2012-09-28 20:14:22
【问题描述】:

在我的 CakePHP 应用程序中,我连接了以下路由:

Router::connect('/:city/dealer/:id', 
    array('controller' => 'dealers', 'action' => 'view'),
    array(
        'pass' => array('city', 'id'),
        'city' => '[a-z]+',
        'id' => '[0-9]+'
        )
    );

这很好用并且可以启用:domain.com/washington/dealer/1

但是如何在视图中为该 URL 生成正确的 HTML 链接?如果我这样做:

echo $this->Html->link(
    'Testlink',
    array('washington', 'controller' => 'dealers', 'action' => 'view', 1)
);

它将所有参数添加到生成链接的末尾:

http://domain.com/dealers/view/washington/1

我该如何正确地做到这一点?

【问题讨论】:

  • 如果您在路线中使用 '/:city/:controller/:id' 会发生什么?您希望其他控制器使用相同的模式吗?
  • 当我使用 '/:city/:controller/:id' 时,Cake 抱怨 domain.com/washington/dealer/1 缺少 DealerController ... App 中基本上只有这个控制器 ...
  • 试试 domain.com/washington/dealers/1(注意经销商上的 s)
  • 复数形式有效,但这对链接构建没有帮助。
  • 好吧,我有点让它与绝对网址一起工作:$this->Html->link(Link, '/washington/dealers/'.$id.''); 这似乎是一个非常糟糕的做法,因为如果我改变路线,它就会停止工作。如果有人知道更好的方法,我会很高兴。

标签: cakephp routing reverse routes view-helpers


【解决方案1】:

嗨,塞巴斯蒂安,现在帮助你可能为时已晚,但我也许可以帮助其他人解决这个问题。解决问题的关键是在 Helper 类中添加 url 方法。我通过在 View/Helper 中创建 AppHelper.php 来做到这一点。它看起来像这样。我为您的城市更改了参数。

查看/Helper/AppHelper.php

<?php
App::uses('Helper', 'View');
class AppHelper extends Helper {

    function url($url = null, $full = false) { 
            if (is_array($url)) { 
                   if (empty($url['city']) && isset($this->params['city'])) { 
                            $url['city'] = $this->params['city']; 
                    }

                    if (empty($url['controller']) && isset($this->params['controller'])) { 
                            $url['controller'] = $this->params['controller']; 
                    }

                    if (empty($url['action']) && isset($this->params['action'])) { 
                            $url['action'] = $this->params['action']; 
                    }
            } 

            return parent::url($url, $full); 
    }

 }
 ?>

然后我创建类似的路线

Router::connect('/:city/dealer/:id', 
array('controller' => 'dealers', 'action' => 'view', 'id'=>':id'),
array('pass' => array('city', 'id'),
      'city' => '[a-z]+',
      'id' => '[0-9]+'
));

希望这会有所帮助:)

【讨论】:

    【解决方案2】:

    我相信您仍然需要指定参数,如下所示:

    echo $this->Html->link('Testlink',
        array('controller' => 'dealers', 'action' => 'view', 'city' => 'washington',
                                                             'id'=> 1));
    

    Cake 在食谱中有一个类似的例子:

    <?php
    // SomeController.php
    public function view($articleId = null, $slug = null) {
        // some code here...
    }
    
    // routes.php
    Router::connect(
        '/blog/:id-:slug', // E.g. /blog/3-CakePHP_Rocks
        array('controller' => 'blog', 'action' => 'view'),
        array(
            // order matters since this will simply map ":id" to $articleId in your action
            'pass' => array('id', 'slug'),
            'id' => '[0-9]+'
        )
    );
    
    // view.ctp
    // this will return a link to /blog/3-CakePHP_Rocks
    <?php
    echo $this->Html->link('CakePHP Rocks', array(
        'controller' => 'blog',
        'action' => 'view',
        'id' => 3,
        'slug' => 'CakePHP_Rocks'
    ));
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2020-01-23
    相关资源
    最近更新 更多