CakePHP在URL中使用Id和Title

时间:2011-10-10 20:58:40

标签: php cakephp

我想使用id和赞成标题创建类似:/favours/20201/This_is_a_favour的链接。

我设置了如下链接:

<h3><?php echo $this->Html->link($favour['Favour']['title'], array('controller'=>'favours','action'=>'view',$favour['Favour']['id'], Inflector::slug($favour['Favour']['title']))); ?></h3>

和控制器动作SO FAR是:

function view ( $id, $title )
{
    $favour = $this->Favour->find('first', array(
        'conditions' => array('Favour.title' => $title)
    ));

    if (empty($favour))
    {
        $this->cakeError('error404');
    }
    $this->set(compact('favour'));
}

Router::connect('/favours/:id/:favourName',array('controller'=>'favours','action'=>'view'),
        array(
            'favourName'=>'[A-Za-z0-9\._-]+',
            'pass'=>array('favourName')
        ),
        array('id' => '[0-9]+')
    );

但是我现在得到了404?

注意:我不想在数据库中使用sluggable插件或存储slu!

4 个答案:

答案 0 :(得分:3)

首先,您需要在routes.php中设置路线:

Router::connect('/favours/:id',
                array('controller' => 'favors', 'action' => 'view'),
                array('id' => '[0-9]+', 'pass' => array('id')));

Router::connect('/favours/:id/:slug',
                array('controller' => 'favors', 'action' => 'view'),
                array(
                    'id'   => '[0-9]+',
                    'slug' => '[A-Za-z0-9\._-]+',
                    'pass' => array('id', 'slug')
                ));

然后我建议在你的Favor模型中添加一个处理:slug参数创建的方法,因为你必须在不同的地方使用它,你可能想要改变它的工作原理后:

public static function slug(array $favour) {
    // good enough for now, might want to change this later
    return Inflector::slug($favour['title']);
}

然后,无论何时建立链接,都要这样做:

array('controller' => 'favours', 'action' => 'view',
      'id' => $favour['Favour']['id'], 'slug' => Favour::slug($favour['Favour']))

你的控制器动作看起来像这样:

public function view($id, $slug) {
    $favour = $this->Favour->find('first', array('conditions' => array('Favour.id' => $id)));
    if (!$favour) {
        $this->cakeError('error404');
    }
    if (Favour::slug($favour['Favour']) != $slug) {
        $this->redirect(array('id' => $id, 'slug' => Favour::slug($favour['Favour'])));
    }

    $this->set(compact('favour'));
}

答案 1 :(得分:0)

Mark Story(CakePHP的核心开发人员)写了如何做你想做的事情 - 他的文章中的一些小调整可以让你在那里做得很好(不要被他的帖子标题沮丧)。 http://mark-story.com/posts/view/using-custom-route-classes-in-cakephp

答案 2 :(得分:0)

为什么这么复杂? 只需使用规范 - 这就是它的用途

/controller/action/id/some-slugged-title

是规范链接 - 但

/controller/action/id/

/controller/action/id/some-slugged-ti

/controller/action/id/some-slugged-title-blabla

一切都会奏效。通过规范,搜索引擎知道正确的是什么

SEO在这里很好。并且不需要额外的重定向(减少服务器消耗)。 此外,如果slug / title更改,您将无法获得404。 这是许多其他解决方案的巨大风险。

答案 3 :(得分:0)

    function view ( $id = null, $slug = null )
    {
        $this->Favour->id = $id;
        $this->set('favour', $this->Favour->read());
    }

    Router::connect('/favours/:id/:slug',array('controller'=>'favours','action'=>'view'),
        array(
            'pass' => array('id','slug'),
            'id' => '[0-9]+'
        )
    );

<?php echo $this->Html->link($favour['Favour']['title'], array('controller'=>'favours','action'=>'view','id'=>$favour['Favour']['id'], 'slug'=>Inflector::slug($favour['Favour']['title']))); ?>