CakePHP控制器方法变量

时间:2014-09-29 11:09:36

标签: cakephp cakephp-2.5

我正在尝试定义一个将传递给Controller的Action的变量,如下所示

public function index($branch = null) {
        //padmanabha-nagar
        //jp-nagar
        $branch = "padmanabha-nagar";

        $this->loadModel('Branch');

        // Check if the Branch is valid
        if(!empty($branch)){
            $this->Branch->recursive = -1;
            $branch_result = $this->Branch->find('first', array('order' => array('Branch.name ASC'), 'conditions' => array('Branch.slug' => $branch)));

          //debug($branch_result);

            // Fetch the Branch ID
            if(!empty($branch_result)){
                $branch_id = $branch_result['Branch']['id'];
            } else {
                return $this->redirect(array('controller' => 'pages', 'action' => 'home'));
            }

            // Fetch the Gallery Images
            $this->Gallery->recursive = -1;
            $this->Paginator->settings = $this->paginate;
            $galleries = $this->Gallery->find('all', array('conditions' => array('Gallery.branch_id' => $branch_id)));

            //debug($galleries);
        }

        $this->set(compact('galleries', 'branch_result'));
    }

但是当我尝试访问$ branch时,似乎没有设置。我需要做任何其他配置才能获得内容吗?

3 个答案:

答案 0 :(得分:1)

尝试将分支添加到此行,如下所示:         $ this-> set(compact('galleries','branch_result','branch'));

答案 1 :(得分:1)

您正在编写输入变量

public function index($branch = null) {
        //padmanabha-nagar
        //jp-nagar
        $branch = "padmanabha-nagar"; <= you are over writing the input variable
        ...

你可以这样做

        $branch = !empty($branch)?$branch:"padmanabha-nagar";

答案 2 :(得分:1)

您可以使用

创建一个变量来分类
  $branches = null; 

而不是动作,然后使用

变量
  $this->branches