开发者

Symfony generation

开发者 https://www.devze.com 2023-01-28 13:08 出处:网络
When I generate module in symfony 1.4, it creates (for example) \'New\' and \'Create\' methods like this:

When I generate module in symfony 1.4, it creates (for example) 'New' and 'Create' methods like this:

public function executeNew(sfWebRequest $request)
{
  $this->form = new SomeForm();
}

public function executeCreate(sfWebRequest $request)
{
  $this->forward404Unless($request->isMethod(sfRequest::POST));

  $this->form = new SomeForm();
  $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
  if ($form->isValid())
  {
    $res_object = $form->save();
    $this->redirect('results_show', $res_object);
  }

  $this->setTemplate('new');
}

With --non-verbose-templates it generates something like the code below (I modified it to show only the creation part):

public function executeNew(sfWebRequest $request)
{
  $this->forward404Unless($request->isMethod(sfRequest::POST));

  $this->form = new SomeForm();
  if ($request->isMethod(sfRequest::POST)) {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($fo开发者_运维知识库rm->getName()));
    if ($form->isValid())
    {
      $res_object = $form->save();
      $this->redirect('results_show', $res_object);
    }
  }
}

What's the reason of such default generation? I think that method below is more compact, adds lesser routes and actions and doesn't change URL for first and next (if there were errors) form displaying. But the default generation method should be the best or the most often usable. So, what do guys from Sensio Labs know, but I don't?


The first template is better suited to building a RESTful application with Symfony.

If you want to use the clever sfObjectRouteCollection class for creating a collection of routes then both the executeNew and executeCreate methods would be expected.

sfObjectRouteCollection by default creates routes for the following actions:

list, new, create, edit, update, delete, and show.

You are correct that your preferred template has only one URL but really this is not a problem if you are using Doctrine/Propel forms and then the helper form_tag_for(). This helper takes the form and name of the route collection as arguments then figures out for itself which is the the correct route/url to use from the collection.

0

精彩评论

暂无评论...
验证码 换一张
取 消