Give's a Symfony2 controller a simple wizard/workflow with annotations.
// composer.json
{
"require": {
"ibrows/wizard-annotation-bundle": "*"
}
}
$ php composer.phar update ibrows/wizard-annotation-bundle
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Ibrows\Bundle\WizardAnnotationBundle\IbrowsWizardAnnotationBundle(),
);
}
<?php
namespace YourApp\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Ibrows\Bundle\WizardAnnotationBundle\Annotation\Wizard;
/**
* @Route("/wizard")
*/
class WizardController extends Controller
{
/**
* @Route("/register", name="wizard_register")
* @Template
* @Wizard(name="register", number=1)
* @return array
*/
public function registerAction()
{
return array();
}
/**
* @Route("/address", name="wizard_address")
* @Template
* @Wizard(name="address", number=2, validationMethod="addressValidation")
* @return array
*/
public function addressAction()
{
return array();
}
/**
* @return bool
*/
public function addressValidation()
{
/**
* Do your checks if this step is valid and return a Response/RedirectResponse or Wizard::REDIRECT_STEP_BACK
* if something is wrong. Otherwise return true
*/
if(!$this->getUser()){
return Wizard::REDIRECT_STEP_BACK;
}
return true;
}
/**
* @Route("/summary", name="wizard_summary")
* @Template
* @Wizard(name="summary", number=3, validationMethod="summaryValidation")
* @return array
*/
public function summaryAction()
{
return array();
}
/**
* @return bool
*/
public function summaryValidation()
{
/**
* Do your checks if this step is valid and return a Response/RedirectResponse or Wizard::REDIRECT_STEP_BACK
* if something is wrong. Otherwise return true
*/
if(!$this->someCheck()){
return new RedirectResponse($this->generateUrl('index'));
}
if(!$this->someOtherCheck()){
return new RedirectResponse($this->generateUrl('login'));
}
return true;
}
/**
* @Route("/display", name="wizard_display")
* @Template
* @return array
*/
public function displayAction()
{
return array(
'wizard' => $this->get('ibrows_wizardannotation.annotation.handler')
);
}
}
Just render the wizardAction in the view:
{% render "YourBundle:Wizard:display" %}
And the diplay.html.twig:
{% extends 'IbrowsWizardAnnotationBundle:Wizard:base.html.twig' %}