Can't pass parameter in symfony 1.0.17
Apologies in advance -- symfony amateur here with an inherited project.
I've added the following to 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:
$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 doe开发者_运维知识库s not.
Parameter, y u 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');
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. 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. To get the request's parameter holder you would use $sf_request
from the template. 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
精彩评论