Symfony - Form requested url not found

2020-04-20 11:47发布

问题:

Im working on a symfony project. Im battleling with a form that won't redirect to its own page. The action attribute is set to "" and method set to post. In that case it should call the same page but I'm ending on a 404 page.

Here's the code of my page in the action file:

public function executeDetail(sfWebRequest $request) {

if($request->isMethod(sfRequest::POST))
{

        if(!$this->getUser()->isAuthenticated())
                $this->redirect('@user_login');

        $formData = $request->getParameter($this->form->getName());

    $this->form->bind($formData, $request->getFiles($this->form->getName()));

            if ($this->form->isValid())
    {
        $user = $this->getUser()->getLogged();  

        $comment = $this->form->save();
                $comment->setIsActive(1);
                $comment->setAuthor($user);
                $comment->setHash(md5(uniqid(rand(), true)));
                $comment->setArticle($this->detail);
                $comment->save();

                $this->status = 'SUCCESS';



    }
    else
    {
        $this->status = 'ERROR';
    }

}
         $this->story = $this->getRoute()->getObject();
    $this->status = false;
    $this->bAuthorLogged = false;
$this->form = new ArticleCommentForm();
} 

The funny thing is that when I call the page from it's url it's correctly show up, 404 only happens when submitting with the form.

Thanks in advance

PS: routing config is:

stories_detail:
  url:   /stories-of-the-month/:slug
  class:   sfDoctrineRoute
  param: { module: stories, action: detail}
  options: { model: Article, type: object, method: doSelectForSlug }

回答1:

You need to explicitly allow POST for the route. Change your route to:

stories_detail:
  url:   /stories-of-the-month/:slug
  class:   sfDoctrineRoute
  param: { module: stories, action: detail}
  options: { model: Article, type: object, method: doSelectForSlug }
  requirements: 
    sf_method: [get, post]