ibrows / wizard-annotation-bundle
带有验证方法的控制器简单向导
1.3.0
2016-04-12 16:26 UTC
Requires
- php: >=5.3.0
- symfony/framework-bundle: >=2.0
README
为Symfony2控制器提供一个简单的带有注解的向导/工作流程。
如何安装
将Bundle添加到你的composer.json文件中
// composer.json { "require": { "ibrows/wizard-annotation-bundle": "*" } }
使用composer.phar从控制台安装Bundle
$ php composer.phar update ibrows/wizard-annotation-bundle
在AppKernel.php中启用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') ); } }
显示向导
只需在视图中渲染wizardAction即可
{% render "YourBundle:Wizard:display" %}
以及diplay.html.twig
{% extends 'IbrowsWizardAnnotationBundle:Wizard:base.html.twig' %}