mero / base-bundle
此包已被废弃且不再维护。未建议替代包。
具有额外功能的 Symfony 扩展包。
2.3.0
2017-05-28 23:06 UTC
Requires
- php: >=5.4.9
- ext-intl: *
Requires (Dev)
- doctrine/orm: ^2.4.8
- phpunit/phpunit: ~4.4
- satooshi/php-coveralls: ~1.0
- symfony/symfony: ~2.8
Suggests
- mero/br-validator-bundle: Bundle for Symfony with validators for Brazilian location
- mero/telegram-handler: Monolog handler to send log via Telegram
README
具有额外功能的 Symfony 扩展包。
要求
- PHP 5.4.9 或更高版本
- Symfony 2.7 或更高版本(包括 Symfony 3)
使用 Composer 安装
- 打开您的项目目录;
- 运行
composer require mero/base-bundle
以将 MeroBaseBundle 添加到您的项目 vendor; - 打开 my/project/dir/app/AppKernel.php;
- 添加
Mero\Bundle\BaseBundle\MeroBaseBundle()
。
Symfony 验证器
适用 | 选项 | 类 | 验证器 | 描述 |
---|---|---|---|---|
属性或方法 | 消息 | DateRange | DateRangeValidator | 验证日期范围 |
基本用法
<?php use Mero\Bundle\BaseBundle\Validator\Constraints as MeroAssert; class Payment { /** * @var \DateTime Payment date * * @MeroAssert\DateRange(min="2017-01-01", max="today") */ private $date; }
新版本中的巴西验证器
巴西验证器功能已迁移到包 mero/br-validator-bundle。
AbstractController 到 Symfony 控制器
具有基本方法的抽象控制器,便于识别框架资源。
名称 | 属性 | 返回类型 | 描述 |
---|---|---|---|
getCurrentRequest | - | 请求 | 获取当前请求 |
getActionName | - | 字符串 | 获取操作名称 |
getBundleName | 请求 $request | 字符串 | 获取包名称 |
getRouteName | 请求 $request | 字符串 | 获取路由名称 |
wsResponse | $data, int $status, array $headers, string $format | 响应 | 返回新的 JSON 或 XML 响应 |
用法示例
namespace Acme\Bundle\BlogBundle\Controller; use Mero\Bundle\BaseBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class NewsController extends AbstractController { /** * @Route("/", name="news") */ public function indexAction(Request $request) { $routeName = $this->getRouteName($request); // Return "news" $actionName = $this->getActionName($request); // Return "indexAction" $bundleName = $this->getBundleName(); // Return "AcmeBlogBundle" $data = [ 'data' => [ 'news' => [ [ 'title' => 'Lorem ipsum' ] ] ] ]; //to return JSON return $this->wsResponse($data, 200, [], AbstractController::WS_RESPONSE_JSON); //to return XML return $this->wsResponse($data, 200, [], AbstractController::WS_RESPONSE_XML); } }
Doctrine ORM 实体
名称 | 类型 | 描述 | 地址 |
---|---|---|---|
IdTrait | 特质 | 创建主键整数字段 | Mero\Bundle\BaseBundle\Entity\Field\IdTrait |
UuidTrait | 特质 | 创建主键 UUID 字段 | Mero\Bundle\BaseBundle\Entity\Field\UuidTrait |
CreatedTrait | 特质 | 创建存储创建日期的字段 | Mero\Bundle\BaseBundle\Entity\Field\CreatedTrait |
ModifiedTrait | 特质 | 创建存储最后更改日期的字段 | Mero\Bundle\BaseBundle\Entity\Field\ModifiedTrait |
AbstractEntity | 抽象类 | 使用整数标识符的经典实体超类 | Mero\Bundle\BaseBundle\Entity\AbstractEntity |
AbstractEntityWithUuid | 抽象类 | 使用 UUID 标识符的实体超类 | Mero\Bundle\BaseBundle\Entity\AbstractEntityWithUuid |
使用 AbstractEntity 的示例
namespace Acme\Bundle\BlogBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Mero\Bundle\BaseBundle\Entity\AbstractEntity; /** * @ORM\Entity() * @ORM\Table(name="post") */ class Post extends AbstractEntity { // Entity class with IdTrait, CreatedTrait and ModifiedTrait implemented }
使用 AbstractEntityWithUuid 的示例
namespace Acme\Bundle\BlogBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Mero\Bundle\BaseBundle\Entity\AbstractEntityWithUuid; /** * @ORM\Entity() * @ORM\Table(name="post") */ class Post extends AbstractEntityWithUuid { // Entity class with UuidTrait, CreatedTrait and ModifiedTrait implemented }
使用特质示例
namespace Acme\Bundle\BlogBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Mero\Bundle\BaseBundle\Entity\IdTrait; /** * @ORM\Entity() * @ORM\Table(name="post") */ class Post { use IdTrait; }
Twig 扩展
国家
根据ISO 3166-1 alpha 2获取国家名称。例如:entity.getCountry()是一个由表单类型国家生成的值。
{# Returns the country name in the server language #} {{ entity.getCountry()|country() }} {# Returns the country name in english #} {{ entity.getCountry()|country('en_US') }} {# Returns the country name in portuguese #} {{ entity.getCountry()|country('pt_BR') }}
语言
根据Unicode语言标识符获取语言名称。例如:entity.getLanguage()是一个由表单类型语言生成的值。
{# Returns the language name in the server language #} {{ entity.getLanguage()|language() }} {# Returns the language name in english #} {{ entity.getLanguage()|language('en_US') }} {# Returns the language name in portuguese #} {{ entity.getLanguage()|language('pt_BR') }}
辅助函数
className
获取类的名称。此辅助函数是为了方便使用PHP 5.4的应用程序而添加的。如果你使用的是PHP 5.5或更高版本,则不建议使用此功能,因为语言本身已经添加了原生实现。
namespace Acme\Bundle\BlogBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Mero\Bundle\BaseBundle\Helper\ClassNameTrait; /** * @ORM\Entity() * @ORM\Table(name="post") */ class Post { use ClassNameTrait; } Post::className(); // Return "Post" using ClassNameTrait for PHP 5.4 Post::class // Return "Post" if you are using PHP 5.5 or above