-1

I'm a function, CakePHP and OOPHP noob so please be patient with me ;)

I have a 301 redirect script I use in the view function of the controllers to compare the slug in the url to the slug in the db and if they don't match 301 redirect to the url with the right slug

I'm trying to turn it into custom functions but I haven't had much success...

This is the code:

$pieces = explode('__', $this->params['pass'][0]);

    if (!isset($pieces[2])) {
        if ($pieces[1] != $this->Smartphone->field('slug')) {
            $this->redirect(array('action' => 'view', $this->Smartphone->field('id').'__'.$this->Smartphone->field('slug')));
        }
    }

My guess is a function would look something like:

public function 301redirect($model) {
    $pieces = explode('__', $this->params['pass'][0]);

if (!isset($pieces[2])) {
    if ($pieces[1] != $this->Smartphone->field('slug')) {
        $this->redirect(array('action' => 'view', $this->$model->field('id').'__'.$this->$model->field('slug')));
    }
}

}

Can you guys tell me what I should put where to get this function to work?

Thnx!

4

1 回答 1

0

这听起来像是自定义组件的工作(http://book.cakephp.org/2.0/en/controllers/components.html#creating-a-component

组件允许您将功能打包在一起并在控制器之间共享它们。

于 2012-08-20T14:50:29.493 回答