phpcakephp

CakePHP: Unable to pass multiple parameters from element to controller


I'm trying to pass two parameters from my view to a controller in CakePHP, but only the first parameter is being received.

What I'm doing:

In my view, I'm using the following code to render an element with two parameters:

<?php
echo $this->element('produtos-categoria', array(
    'categoria_id' => $produtos['Produto']['categoriasproduto_id'],
    'produto_id' => $produtos['Produto']['id']
));
?>

The problem:

In my controller, I'm trying to access both parameters, but only $categoria_id has a value. The $produto_id parameter is empty:

public function list_categories($categoria_id = null, $produto_id = null) {
    pr($produto_id); // This is empty
    exit;
}

What I've tried:

I've verified that both values (categoria_id and produto_id) exist in the view before passing them to the element.

Question:

How can I properly pass and access both parameters from my element to the controller in CakePHP?


Solution

  • I donno what are you trying to achieve but to get the second variable you would need to set the second parameter to your method.

    // If you want to set variable from a function and get it from an other function
    public function an_other_function(){
        $this->listacategorias(22, 333);
    }
    
    public function listacategorias($categoria_id = null, $produto_id = null ) {
        var_dump($categoria_id);
        var_dump($produto_id);
    }
    
    
    // If you want to set and get variable from url
    host_or_domain_name/controller_name/listacategorias/22/33