CakePHP:缺少参数错误,通过URL传递参数

时间:2012-07-11 06:13:20

标签: php cakephp

这是我的控制器(显然未完成但是“做一些魔术”我首先需要让它运行):

    public function add($customer_id, $question_set_id, $order_value) {

        $customer_id = $this->params['url']['customer_id'];
        $question_set_id = $this->params['url']['question_set_id'];
        $order_value = $this->params['url']['order_value'];

        $possible_answer_model = ClassRegistry::init('PossibleAnswer');
        $question_model = ClassRegistry::init('Question');
        $order_model = ClassRegistry::init('Order');

        $order = $order_model -> find('first', array(
        'Order.question_set_id' => $question_set_id,
        'Order.value' => $order_value));

        $question = $question_model -> find('first', array(
        'Question.id' => $order['Order']['question_id']));

        $this -> set('question', $question);

        if ($question['Question']['kind'] != "o") {
            $this -> set('possible_answers', $possible_answer_model -> find('all', array(
            'PossibleAnswer.question_id' => $question['Question']['id'])));
        }
.
.
.

我得到的是:

  

警告(2):AnswersController缺少参数1 :: add()[APP \ Controller \ AnswersController.php,第10行]

     

警告(2):AnswersController缺少参数2 :: add()[APP \ Controller \ AnswersController.php,第10行]

     

警告(2):AnswersController缺少参数3 :: add()[APP \ Controller \ AnswersController.php,第10行]

我的链接是:

  

/答案/添加/ CUSTOMER_ID = 1&安培; question_set_id = 1&安培; ORDER_VALUE = 1

3 个答案:

答案 0 :(得分:3)

链接需要

/answers/add/1/1/1

以便映射到

AnswersController::add($customer_id, $question_set_id, $order_value)

必须通过控制器内的$this->params访问常规查询参数,它们不会作为参数传递给操作。

答案 1 :(得分:0)

 public function add($customer_id=null, $question_set_id=null, $order_value=null) {
  ....
 }

可能不会在需要时传递您需要的参数,您可以执行上述操作,但不会要求它。

答案 2 :(得分:0)

我使用自定义路线,这解决了我。

Router::connect('/me/edit/:id', array('controller' => 'users', 'action' => 'edit'),
        array('pass' => array('id'), 'id' => '[0-9]+')
    );

感谢https://stackoverflow.com/a/17989140/1424473中的@ david-yell。