doutuzhuohao6449 2015-11-19 12:35
浏览 82

在Zend Framework 2中获取参数

I am trying to get a particular parameter with Zend Framework 2. For example,

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

Would $this->getRequest()->getParam('id') return the id number from the page url? I have the route set up to be in the format of:

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

So if a form was submitted (via POST) and the corresponding url was /admin/edit-template/3 would $this->getRequest()->getParam('id') contain 3?

Thanks!

  • 写回答

1条回答 默认 最新

  • doze79040 2015-11-20 21:57
    关注

    If you want to take param from post maybe it's better to use: $this->params()->fromPost('param_name');

    If you want from route: $this->params()->fromRoute('id');

    You can create with 'id' input name, and you can use id from route.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?