To improve the workflow sometimes it may be better to redirect the user back to the list after the Save-action. This tutorial shows how to implement this in Symfony 1.3 and 1.4

First copy the "processForm"-function from the action.class.php in your cache-directory ( /cache/dev/modules/auto[Modulename]/actions/actions.class.php ) to your action.class.php and change the lines:

  if ($request->hasParameter('_save_and_add'))
  {
    $this->getUser()->setFlash('notice', $notice.' You can add another one below.');

    $this->redirect('@[Modulename]_new');
  }      
  else
  {
    $this->getUser()->setFlash('notice', $notice);

    $this->redirect(array('sf_route' => '[Modulename]_edit', 'sf_subject' => $[Modulename]));
  }

to:

  if ($request->hasParameter('_save_and_add'))
  {
    $this->getUser()->setFlash('notice', $notice.' You can add another one below.');

    $this->redirect('@[Modulename]_new');
  }
  else if ($request->hasParameter('_save_and_list'))
  {
    $this->getUser()->setFlash('notice', $notice);

    $this->redirect('@[Modulename]');
  }
  else
  {
    $this->getUser()->setFlash('notice', $notice);

    $this->redirect(array('sf_route' => '[Modulename]_edit', 'sf_subject' => $[Modulename]));
  }

Open the file /apps/[Appname]/modules/[Modulename]/lib/[Modulename]GeneratorHelper.class.php and add the function:

public function linkToSaveAndList($object, $params)
{
  return '<li class="sf_admin_action_save_and_list"><input type="submit" value="'.__($params['label'], array(), 'sf_admin').'" name="_save_and_list" /></li>';
}

In /apps/[Appname]/modules/[Modulename]/config/generator.yml:

...
config:
  ...
  form:
    actions:
       save_and_list: ~
       ...

Now there is a new button "Save and list" at the bottom of the Edit-form that will redirect the user to the list after saving the form. Maybe it would be useful to change the generator-classes to auto generate this button for every module.

gepostet am 15.11.2010 von Julian Stricker (Google+) Tags: php symfony programmieren