如何告诉cakephp使用函数索引($ type)来转到索引页面?

时间:2012-02-07 15:28:29

标签: cakephp-2.0

我有这个应用程序,用这个功能将用户引导到景点类型:

public function index($type=null) {
        $this->set('title','What to do when you visit Gulf Shores');

        $this->paginate['Attraction']=array(
                'limit'=>9,
                'order'=>array('Attraction.id'=>'asc'),
                'conditions'=>array(
                        'active'=>1,
                        'attr_type'=>$type
                        )
                );
        $c=$this->paginate('Attraction');
        $this->set('attractions', $c);

}

并且效果很好,但我希望用户也可以访问不会被attr_type过滤掉的首页/景点/。此函数显示首页的结果为零(显然$ type still = null)。是否有一个我缺少的步骤,或者我的控制器中是否有view.ctp文件和功能?

1 个答案:

答案 0 :(得分:0)

您可以使用if语句来确定条件:

public function index($type = null) {
    $this->set('title', 'What to do when you visit Gulf Shores');

    $conditions = array();  //create $conditions outside of the if statement
    if ($type) {    //if $type is equal to anything other than null or 0
        $conditions = array(
            'active' => 1,
            'attr_type' => $type
        );
    } else {
        $conditions = array(
            'active' => 1
        );
    }

    $this->paginate['Attraction'] = array(
        'limit' => 9,
        'order' => array('Attraction.id' => 'asc'),
        'conditions' => $conditions
    );
    $c = $this->paginate('Attraction');
    $this->set('attractions', $c);
}

实际上并不需要在PHP中的if语句之外创建$conditions,但由于范围的原因,它在很多其他编程语言中。

If you create a variable inside a if statement is it available outside the if statement?