mb-x / architect-bundle
此包提供了一种架构,用于将您的 Symfony 应用程序中的不同工作层分离。
v0.3.0
2016-12-11 17:24 UTC
Requires
- php: >=5.4.0
- doctrine/doctrine-bundle: *
- symfony/framework-bundle: >=2.7
- twig/twig: *
This package is not auto-updated.
Last update: 2024-09-27 22:58:32 UTC
README
此包提供了一种架构,用于将您的 Symfony 应用程序中的不同工作层分离。
1. 安装
此包已在 Symfony 2.8.14 和 Symfony 3.0.9 上进行测试
步骤 1: 使用 composer 下载 ArchitectBundle
使用 composer 需求包
$ composer require mb-x/architect-bundle
步骤 2: 启用包
在内核中启用包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Mbx\ArchitectBundle\MbxArchitectBundle(), ); }
2. 使用
步骤 1: 实现 EntityInterface
首先,您的实体类应实现 EntityInterface 和 getId 方法
<?php namespace AppBundle\Entity; use Mbx\ArchitectBundle\Interfaces\EntityInterface; class Post implements EntityInterface { // ... public function getId() { return $this->id; } // ... }
步骤 2: 创建 Manager 和 FormHandler 类
$ php app/console mbx:generate AppBundle:Post
此命令将为 Post 实体生成 Manager 和 FormHandler 类
步骤 3: 将您的 Manager 和 FormHandler 类注册为服务
appbundle.post_manager: class: AppBundle\Manager\PostManager parent: mbx.abstract_entity_manager appbundle.post_form_handler: class: AppBundle\FormHandler\PostFormHandler parent: mbx.abstract_form_handler arguments: ['@appbundle.post_manager']
步骤 4: 控制器
您的控制器将包含更少的代码,因为所有逻辑和操作都将由您的 Manager 和 FormHandler 类执行
/** * Lists all post entities. * * @Route("/", name="post_index") * @Method("GET") */ public function indexAction() { $posts = $this->get('appbundle.post_manager')->getRepository()->findAll(); return $this->render('post/index.html.twig', array( 'posts' => $posts, )); } /** * Creates a new post entity. * * @Route("/new", name="post_new") * @Method({"GET", "POST"}) */ public function newAction(Request $request) { $post = new Post(); $formHandler = $this->get('appbundle.post_form_handler'); if ($formHandler->processForm($post)) { return $this->redirectToRoute('post_show', array('id' => $post->getId())); } return $this->render('post/new.html.twig', array( 'post' => $post, 'form' => $formHandler->getForm()->createView(), )); } /** * Finds and displays a post entity. * * @Route("/{id}", name="post_show") * @Method("GET") */ public function showAction(Post $post) { $formHandler = $this->get('appbundle.post_form_handler'); return $this->render('post/show.html.twig', array( 'post' => $post, 'delete_form' => $formHandler->createDeleteForm($post)->createView(), )); } /** * Displays a form to edit an existing post entity. * * @Route("/{id}/edit", name="post_edit") * @Method({"GET", "POST"}) */ public function editAction(Request $request, Post $post) { $formHandler = $this->get('appbundle.post_form_handler'); if ($formHandler->processForm($post)) { return $this->redirectToRoute('post_edit', array('id' => $post->getId())); } return $this->render('post/edit.html.twig', array( 'post' => $post, 'edit_form' => $formHandler->getForm()->createView(), 'delete_form' => $formHandler->createDeleteForm($post)->createView(), )); } /** * Deletes a post entity. * * @Route("/{id}", name="post_delete") * @Method("DELETE") */ public function deleteAction(Request $request, Post $post) { $formHandler = $this->get('appbundle.post_form_handler'); if ($formHandler->processDeleteForm($post)) { // $this->get('session')->getFlashBag()->add('Deleted Successfully'); } return $this->redirectToRoute('post_index'); }
3. 示例
4. 建议
类似于其他任何软件,《MbxArchitectBundle》也不是完美的。任何可以改进或添加此包功能的建议都将受到欢迎。
5. 报告问题或功能请求
问题和功能请求在 Github 问题跟踪器 中跟踪。
6. 友好许可
此包在 MIT 许可下提供。请参阅包中的完整许可证。
Resources/meta/LICENSE
您有权使用、修改和分发此软件,只要保留版权头信息(特别是以 /* 开始的注释块)!