Ask your Symfony questions! Pay money and get answers fast! (more info)

Module and action not in request when submitting a form Symfony

  • SOLVED

Hello,

I have quite a strange problem with a Symfony 1.4 project that I haven't been able to solve after doing lots of research and debugging. I think it is something small that I am missing cause I did stuff like this a million times before without a problem. I have a page called storySingle, this page has a form where users can leave a comment.
// storySingleSuccess.php
<form action="<?php echo url_for('@user_story?title='.$story->getSlug()) ?>" method="post">
<?php echo $form ?>
<input type="submit" value='Reactie plaatsen'/>
</form>


The form will submit to itself and saving the form information is handled in the action:
<?php
class profileActions extends sfActions
{

[.....]

public function executeStorySingle(sfWebRequest $request)
{
$title = $request->getParameter('title');
$this->name = $request->getParameter('subdomain');

$this->user = Doctrine::getTable('sfGuardUser')->retrieveByUsername($this->name);
$this->story = Doctrine::getTable('Story')->retrieveByUserIdAndSlug($this->user->getId(), $title);

$options = array(
'story_id' => $this->story->getId(),
);
$this->form = new StoryCommentForm(null, $options);

if ($request->isMethod('post')) {
$this->form->bind($request->getParameter('story_comment'));
if ($this->form->isValid()) {
$comment = $this->form->save();
$this->redirect('@user_story?title='.$this->story->getSlug());
}
}
}
}
?>


My routing file contains the following routes:
homepage:
url: /
class: sfDomainRoute
param: { module: home, action: index }
user_stories:
url: /verslagen
class: sfDomainRoute
param: { module: profile, action: stories }
user_story:
url: /verslag/:title
class: sfDomainRoute
param: { module: profile, action: storySingle }


In another template I have a link to a certain user_story which works fine:
<?php echo link_to($story->getTitle(), '@user_story?title='.$story->getSlug()) ?>

The problem arises when I fill in the form on the storySingle page and press submit. The following exception is thrown:
404 | Not Found | sfError404Exception
Empty module and/or action after parsing the URL "/verslag/title-of-story" (/).


I did put some debugging code in "lib/vendor/symfony/lib/controller/sfFrontWebController.class.php" and found out that both $moduleName and $actionName are empty because apparently they are not in the request. If I check the request ($this->context->getRequest()) it does have other parameters like the post parameters of my form.

Does anybody have a clue why the module and action are absent from the request?

Answers (3)

2010-08-29

Francesco Tassi answers:

Try adding sf_method To ruote definition in routing.yml. If you get 404 posting To a route Maybe you a have To enable post method for that ruote. Check routing reference To get the exact syntax.

It should work.


Wouter Peschier comments:

Thanks! Adding the sf_method did indeed fix the problem :)

2010-08-29

Sergey Kushniruk answers:

You're using sfRequestRoute which extends sfRequestRoute that requires sf_method param to be specified or defaults to GET. So you should specify sf_method param in your routing.yml like this:

user_story:
url: /verslag/:title
class: sfDomainRoute
param: { module: profile, action: storySingle, sf_method: [get, post] }


Wouter Peschier comments:

Thanks! The only thing is that you have to put the sf_method as a requirement instead of as a param ;)

user_story:
url: /verslag/:title
class: sfDomainRoute
param: { module: profile, action: storySingle }
requirements: { sf_method: [get, post] }

2010-08-29

Karol Sójko answers:

Try to erase the class attribute in the routings defined