简体   繁体   English

无法在symfony 1.0.17中传递参数

[英]Can't pass parameter in symfony 1.0.17

Apologies in advance -- symfony amateur here with an inherited project. 提前致歉-Symfony业余爱好者在这里继承了一个项目。

I've added the following to apps/frontend/modules/top/templates/indexSuccess.php: 我已将以下内容添加到apps / frontend / modules / top / templates / indexSuccess.php:

<?php echo form_tag('contents/'.$adv_data->getId()) ?>
<?php echo $sf_params->get('email'); ?>
<?php echo input_tag('email', $sf_params->get('email'))?>
<?php echo submit_tag('Next') ?>
</form>

So then I add the following to apps/frontend/modules/top/actions/actions.class.php in the executeContents() method: 因此,然后在executeContents()方法中将以下内容添加到apps / frontend / modules / top / actions / actions.class.php:

$email = $this->getRequestParameter('email');
$this->getRequest()->setParameter('email', $email);
if (!$this->validateForm()) {
  $this->redirect($adv_id); // sends me back to the page with the form up above
}

I'd expect the email parameter to appear but alas it does not. 我希望email参数会出现,但可惜它不会出现。
Parameter, yu no stay? 参数,没有留下?

First you dont really use the request parameter holder to pass variables to your template. 首先,您实际上并没有真正使用请求参数持有者将变量传递给模板。 Each template will be passed along the variables you assign in the controller. 每个模板将沿着您在控制器中分配的变量传递。 Your action should look like this: 您的操作应如下所示:

// $this->email will be available in the template as $email
// or $sf_params->get('email')
$this->email = $this->getRequestParameter('email');

if (!$this->validateForm()) {
  // pass either module/controller and params or route name and params... 
  // just like link_to() helper function
  $this->redirect('@route_name?id='.$adv_id);

}

However what you probably want to do instead of redirecting is to forward back to the action that renders the form, otherwise your post data wont be carried over: $this->forward('module', 'action'); 但是,您可能想做的而不是重定向是转发回到呈现表单的操作,否则您的帖子数据将不会被继承: $this->forward('module', 'action'); The reason for this is that redirect is just like doing header('Location: ...') - it creates an entirely new request, forward on the other hand simply dispatches the same request to a different module/action so all your request paramters in $_GET and $_POST are carried over. 这样做的原因是redirect就像执行header('Location: ...') -它创建一个全新的请求,另一方面, forward只是将同一请求分派到不同的模块/操作,因此所有请求参数$_GET$_POST被结转。 You can read more about this in the Inside the Controller Layer section of the documentation. 您可以在文档的“ 内部控制器层”部分中了解有关此内容的更多信息。

Now the reason youre not seeing the variable is because $sf_params corresponds to the templates parameter holder, not the request's. 现在,您看不到该变量的原因是因为$sf_params对应于模板参数持有者,而不是请求的。 To get the request's parameter holder you would use $sf_request from the template. 要获取请求的参数持有人,您可以使用模板中的$sf_request You can read more about this in the The Basics of Page Creation: Passing Information from the Action to the Template sub section of the documentation. 您可以在“ 页面创建基础:将信息从操作传递到模板”子部分中了解有关此内容的更多信息

You can find the complete documentation for 1.0 here: The Definitive Guide to Symfony 您可以在此处找到1.0的完整文档: Symfony权威指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM