我是一个函数,CakePHP和OOPHP noob,所以请耐心等待我;)
我有一个301重定向脚本,我在控制器的视图功能中使用它来比较url中的slug和db中的slue,如果它们不匹配,301重定向到带有正确slug 的url
我正试图把它变成自定义函数,但我没有取得太大成功。。。
这是代码:
$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')));
}
}
我的猜测是一个函数看起来像:
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')));
}
}
}
你们能告诉我应该把什么放在哪里让这个函数工作吗?
Thnx!
这听起来像是自定义组件的作业(http://book.cakephp.org/2.0/en/controllers/components.html#creating-a组分)
组件允许您将功能打包在一起,并在控制器之间共享。