【问题标题】:CakePHP: missing argument error, passing arguments via URLCakePHP:缺少参数错误,通过 URL 传递参数
【发布时间】:2012-07-11 06:13:20
【问题描述】:

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

    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::add() 的参数 1 [APP\Controller\AnswersController.php,第 10 行]

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

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

我的链接是:

/answers/add/?customer_id=1&question_set_id=1&order_value=1

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    链接需要

    /answers/add/1/1/1
    

    为了映射到

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

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

    【讨论】:

    • 哦,我在食谱的某个地方找到了“我的方式”,所以我可能搞砸了——链接现在可以工作,但我在 $customer_id = $this 处收到“未定义索引”错误->params['url']['customer_id'] 和另外两个 $this->params...
    • 要么是要么。如果您不在 URL 中将它们作为 ?customer_id=1 传递,则它们在 $this->params 中不可用。
    • 不要忘记将它们设为默认= null 以避免引发警告!
    • @mark 我不希望页面在没有这些值的情况下运行,所以警告是可以的 - 我什至没有索引操作,它只是一个操作,由我的软件管理
    • 事情是,他们的 url 可以在没有任何人的参数的情况下被触发 - 你应该在你的控制器中捕获它。但是无论哪种方式都不需要发出警告,因此我的提示!
    【解决方案2】:
     public function add($customer_id=null, $question_set_id=null, $order_value=null) {
      ....
     }
    

    可能没有在需要时传递您需要的参数,您只需执行上述操作,它就不需要了。

    【讨论】:

    • 对不起,现在我已经编辑了我的问题,我通过女巫展示了链接,我正在尝试传递这些参数(这是必要的)。
    【解决方案3】:

    我正在使用自定义路线,这可以解决我的问题。

    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。

    【讨论】:

      猜你喜欢
      • 2018-09-06
      • 1970-01-01
      • 2015-10-10
      • 2014-05-22
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多