peytz/wizard

Symfony2的Wizard组件。

v1.2.0 2012-10-09 14:18 UTC

This package is not auto-updated.

Last update: 2024-09-14 11:15:26 UTC


README

Build Status

这是一个主要用于与Symfony2框架一起使用的简单组件。因此,其中包含一些对该框架的引用。这些引用很容易更改。

许可证

请参阅LICENSE文件了解许可条款。

贡献

任何人都可以发送Pull Requests。但我们保留拒绝不符合此组件目标请求的权利。

请参阅https://github.com/Peytz/Wizard/contributors以获取贡献者列表

安装

安装此组件有三种方法。您可以使用Composer、Phar或直接在您的应用程序中包含代码。

使用Phar

build/peytz-wizard.phar中有一个可用的Phar存档,它包含自己的小型自动加载器,当包含时,会自动在PHP中注册。

<?php

require 'phar://build/peytz-wizard.phar';

var_dump(class_exists('Peytz\Wizard\Wizard'));

使用Composer

您也可以通过以下要求使用Composer安装它。

{
    "require" : {
        "peytz/wizard" : "master-dev"
    }
}

运行测试

$ phpunit

用法

API非常简单。您创建一个Report,它应该是一个Doctrine实体。Report保存您的数据,必须实现ReportInterface

Report是Wizard构造函数参数的要求。Wizard应该被继承以添加自定义步骤或使用DependencyInjection框架将它们注入到Wizard对象中。

Wizard包含多个实现StepInterface的Steps。有一个基本的Step实现可用。

<?php

namespace Vendor\Wizard;

use Peytz\Wizard\Step;
use Vendor\Wizard\Form\CustomFormType;

class CustomStep extends Step
{
    public function getFormType()
    {
        return new CustomFormType();
    }
}
<?php

namespace Vendor\Wizard;

use Peytz\Wizard\Wizard;
use Peytz\Wizard\ReportInterface;
use Vendor\Wizard\CustomStep;

class CustomWizard extends Wizard
{
    public function __construct(ReportInterface $report)
    {
        parent::__construct($report);
        $this->add(new CustomStep());
    }
}
<?php

namespace Vendor\Wizard;

use Peytz\Wizard\ReportInterface;

class Report implements ReportInterface
{
}

使用Symfony Validator组件进行验证的控制器操作实现。验证分组在此非常实用。

<?php

namespace Vendor\Wizard;

class Controller
{
    protected $validator;

    protected $wizard;

    public function myAction($stepIdentifier)
    {
        $step = $this->wizard->get($stepIdentifier);
        $form = $this->createForm($step->getFormType(), $this->wizard->getReport(), array(
            'validation_groups' => array($step->getName()),
        ));

        if ($_POST) {
            $form->bind($_POST);
            if ($form->isValid()) {
                $this->wizard->process($step);

                // You should proberly save some stuff here? And redirect
            }
        }

        return array(
            'form' => $form,
        );
    }
}

示例Symfony2 DIC定义

如果您想在Symfony2中使用DependencyInjection,这是将Wizard及其步骤关联的另一种方式。

使用DIC的强大之处在于,每个StepInterface实现都可以有可选的依赖项。例如,使用程序性构建表单而不是有FormType

<container>
    <services>
        <service id="vendor.wizard.custom" class="Peytz\Wizard\Wizard">
            <argument type="service" id="vendor.wizard.custom.report" />
            <call method="add">
                <argument type="service" id="vendor.wizard.custom.my_step" />
            </call>
        </service>
    </services>
</container>

更新gh-pages分支

$ ./apigen.sh
$ git push