【问题标题】:Getting parameters in Zend Framework 2在 Zend Framework 2 中获取参数
【发布时间】:2015-11-21 05:57:54
【问题描述】:

我正在尝试使用 Zend Framework 2 获取特定参数。例如,

// set the form to be used in the update templates view
    $form = new EmailTemplateForm();

    // gets the form method request (usually post)
    $request = $this->getRequest();

    // check to see if the request was a POST form request
    if ($request->isPost()) {
        // good to go
        // filter the form values now
        $email = new EmailTemplates();

        $form->setInputFilter($email->getInputFilter());

        // set the form data to hold all the values supplied by the form
        // via $request->getPost()
        $form->setData($request->getPost());

        // now we will see if the form is valid
        // we check if it is valid by the email form class we created
        if ($form->isValid()) {
            // it is valid
            // pass the form to data to the filter class via exchangeArray()
            $email->exchangeArray($form->getData());

            $tpl_id = !empty($this->getRequest()->getParam('id'))
            ? $this->getRequest()->getParam('id') : null;

            if ($this->getEmailTemplatesService()->modifyEmailTemplate($email, $tpl_id) === true) {
                // the updated email template was inserted into the database successfully
                // redirect to email template view
                return $this->redirect()->toUrl('/admin/email-template');
            } else {
                // error occured..
                // the error is logged automatically
                // redirect to email template view
                return $this->redirect()->toUrl('/admin/email-template/' . $tpl_id);
            }
        }
    } 

$this->getRequest()->getParam('id') 会从页面 url 返回 id 号吗?我将路线设置为以下格式:

'route'    => '/admin[/][:action][/:id]',

所以如果一个表单被提交(通过 POST)并且对应的 url 是 /admin/edit-template/3 会 $this->getRequest()->getParam('id') 包含 3 吗?

谢谢!

【问题讨论】:

  • 是的,如果post url具有相同的参数它会起作用,为什么你问而不是尝试?
  • 目前无法测试。感谢您的帮助:)

标签: php zend-framework


【解决方案1】:

如果您想从帖子中获取参数,最好使用: $this->params()->fromPost('param_name');

如果你想从路线: $this->params()->fromRoute('id');

您可以使用 'id' 输入名称创建,并且您可以使用路由中的 id。

【讨论】:

    猜你喜欢
    • 2012-11-03
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    相关资源
    最近更新 更多