javihgil / extra-bundle
此包已被放弃,不再维护。未建议替代包。
为 Symfony 项目提供的实用服务和类
v1.0.2
2018-01-31 13:28 UTC
Requires
- doctrine/orm: ^2.2.3
- symfony/config: ~2.5|~3.0|~4.0
- symfony/dependency-injection: ~2.5|~3.0|~4.0
- symfony/form: ~2.5|~3.0|~4.0
- symfony/http-foundation: ~2.5|~3.0|~4.0
- symfony/http-kernel: ~2.5|~3.0|~4.0
- twig/twig: ^1.9
Requires (Dev)
- hexmedia/yaml-linter: 0.1
- javihgil/composer-ci-tools: 1.0.*
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.2.*
- sensiolabs/security-checker: ~3.0
This package is auto-updated.
Last update: 2021-12-21 15:40:20 UTC
README
为 Symfony 项目提供的实用服务和类
安装
composer require javihgil/extra-bundle ~1.0
配置 Bundle
在 app/AppKernel.php 中注册该 Bundle
// app/AppKernel.php
public function registerBundles()
{
return [
// ...
new \Jhg\ExtraBundle\ExtraBundle(),
];
}
控制器扩展
要使用 ExtraController 中的工具,请将控制器从 Jhg\ExtraBundle\Controller\ExtraController 继承,而不是从 Symfony FrameworkBundle 继承。
<?php
namespace AppBundle\Controller;
use Jhg\ExtraBundle\Controller\ExtraController;
class MyController extends ExtraController
{
}
一些有用的方法
getRepository
$this->getRepository('AppBundle:User')->findBy(...)
trans
$this->trans('Some translatable text');
transChoice
$this->transChoice('{0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples', $count);
addFlashTrans
$this->addFlashTrans('error', 'A translatable error message');
表单处理器
表单处理器接口
表单处理器的任务是封装 CrUD 业务操作到一个处理器类中。
所有表单都必须经过验证,然后对包含的数据执行某些操作,通常这些操作是重复的。
使用表单处理器,所有这些操作都在一个方法中,并且可以随时调用。
Doctrine 创建表单处理器
CRUD 控制器中创建 doctrine 实体的处理器。
示例
/**
* @param Request $request
*
* @return Response
*/
public function createAction(Request $request)
{
$element = new Element();
$form = $this->createForm(new ElementType(), $element);
if ($this->get('create_form_handler')->process($form, $request)) {
return $this->redirectToRoute('success_route', ['element', $element->getId()]);
}
$viewData = [
'form' => $form->createView(),
];
return $this->render('ExampleBundle:Element:create.html.twig', $viewData);
}
Doctrine 更新表单处理器
CRUD 控制器中更新 doctrine 实体的处理器。
示例
/**
* @param Element $element
* @param Request $request
*
* @return Response
*/
public function updateAction(Element $element, Request $request)
{
$form = $this->createForm(new ElementType(), $element);
if ($this->get('update_form_handler')->process($form, $request)) {
return $this->redirectToRoute('success_route', ['element', $element->getId()]);
}
$viewData = [
'element' => $element,
'form' => $form->createView(),
];
return $this->render('ExampleBundle:Element:update.html.twig', $viewData);
}
Doctrine 删除表单处理器
CRUD 控制器中删除 doctrine 实体的处理器。
示例
/**
* @param Element $element
* @param Request $request
*
* @return Response
*/
public function deleteAction(Element $element, Request $request)
{
$form = $this->createForm(new ElementType(), $element);
if ($this->get('delete_form_handler')->process($form, $request)) {
return $this->redirectToRoute('success_route');
}
$viewData = [
'element' => $element,
'form' => $form->createView(),
];
return $this->render('ExampleBundle:Element:delete.html.twig', $viewData);
}