-1

I am trying to define a variable that will pass into the Action of a Controller as follows

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'));
    }

But when i try to access $branch it does not seem to be set. Is there any other configuration i need to do so that i can get the content?

Harsha M V
  • 54,075
  • 125
  • 354
  • 529
  • 1
    Your question is a bit unclear. Your problem is that `$branch` doesn't get set from the view to the action? If that's the case, could you post how you are creating the url to call that action too? If that wasn't the problem, could you explain a bit more? – Nunser Sep 29 '14 at 12:11

3 Answers3

1

Try to add branch to this line as follows: $this->set(compact('galleries', 'branch_result', 'branch'));

Paweł
  • 409
  • 3
  • 13
1

You are over writing the input variable

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

You could do this

        $branch = !empty($branch)?$branch:"padmanabha-nagar";
Fury
  • 4,643
  • 5
  • 50
  • 80
1

You can create a variable to class using

  $branches = null; 

instead of to action, then use the variable using

  $this->branches
Abhishek
  • 795
  • 9
  • 20