fakerino/symfony-fakerino

模拟数据生成器 symfony 扩展包

0.1.1 2015-06-16 20:23 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:13:42 UTC


README

#FakerinoBundle

Latest Stable Version Travis Ci License

FakerinoBundle 提供了将 Fakerino 集成到 Symfony2 框架中的功能。
此包还包括 Fakerino 的 Twig 扩展。

更多信息请参阅 官方文档

安装

###1. 安装 symfony-fakerino 将以下依赖项添加到您的项目 composer.json 文件中

    "require": {
        "fakerino/symfony-fakerino": "~0.1"
    }

###2. 安装 ODS 数据样本 (可选但推荐) 使用以下任一选项安装 ODS 数据样本

2.1 自动安装并更新,将脚本添加到您的 composer.json 文件中 (推荐方式)
  "scripts": {
        "post-install-cmd": "vendor/fakerino/fakerino/build/ods vendor/fakerino/fakerino/data",
        "post-update-cmd": "vendor/fakerino/fakerino/build/ods vendor/fakerino/fakerino/data"
    }

注意: 将此脚本作为第一个要执行的脚本。#####2.2 手动运行命令 (在 fakerino composer 安装之后)

$ vendor/fakerino/fakerino/build/ods vendor/fakerino/fakerino/data

3. 初始化包

要初始化包,您需要将其添加到您的 app/AppKernel.php

public function registerBundles()
{
    // ...
  $bundles = array(
           new Fakerino\Bundle\FakerinoBundle\FakerinoBundle(),
    // ...
}

4. 配置 (可选)

如果未设置配置,Fakerino 将使用默认值。
配置示例 config.yml

fakerino:
    config:
        locale: en-GB
        fake:
            fakeMale:
              - titlemale
              - nameMale
              - surname
            fakeFemale:
              - titlefemale
              - namefemale
              - surname
        database:
            dbname: mydb
            user: username
            password: password
            host: localhost
            driver: pdo_mysql

示例

控制器

<?php
namespace Acme\DemoBundle\Controller;

use Fakerino\Core\FakeDataFactory;
use Symfony\Component\HttpFoundation\Response;
use Twig_Environment;

class HelloController
{
    public function __construct(FakeDataFactory $fakerino, Twig_Environment $twig)
    {
        $this->fakerino = $fakerino;
        $this->twig = $twig;
    }

    public function helloAction()
    {
        $person = $this->fakerino->fake('fakeFemale');
        $duty = $this->fakerino->fakeTemplate('<p>Remeber the appointment with {{ surname }} in {{ country }}</p>');

        return new Response('<html><body> Hello '.$person.'!'.$duty.'</body></html>');
    }

    public function twigAction()
    {
        return new Response(
            $this->twig->render('AcmeDemoBundle:Demo:my_fakerino_demo.html.twig')
        );
    }
}  

Twig 文件

{# Resources/view/Demo/my_fakerino_demo.html.twig #}

Hello Mr {{fake('surname')}}

服务配置

//config/services.xml
<service id="hello_service" class="Acme\DemoBundle\Controller\HelloController">
    <argument type="service" id="fakerino" />
    <argument type="service" id="twig" />
</service>

有关此示例中使用的服务配置和控制器的更多信息,请参阅官方 Symfony 文档中的
服务容器控制器作为服务

输出

helloAction() 的输出,将在每次页面刷新时更改

Hello Ms Adeline Douglas !

Remeber the appointment with Watts in Cyprus

twigAction() 的输出,将在每次页面刷新时更改

Hello Mr Wallace