ArchitectBundle
This Bundle provides an architecture to separate the different job layers in your Symfony application., (*1)
1. Installation
This bundle was tested with Symfony 2.8.14 and Symfony 3.0.9, (*2)
Step 1: Download ArchitectBundle using composer
Require the bundle with composer:, (*3)
``` bash
$ composer require mb-x/architect-bundle, (*4)
### Step 2: Enable the bundle
Enable the bundle in the kernel:
``` php
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Mbx\ArchitectBundle\MbxArchitectBundle(),
);
}
2. Usage
Step 1: Implement EntityInterface
First, your entity class should implement EntityInterface and getId method
``` php
<?php
namespace AppBundle\Entity;, (*5)
use Mbx\ArchitectBundle\Interfaces\EntityInterface;, (*6)
class Post implements EntityInterface
{
// ..., (*7)
public function getId()
{
return $this->id;
}
// ...
}, (*8)
### Step 2: Create the Manager and FormHandler classes
``` bash
$ php app/console mbx:generate AppBundle:Post
this command will generate the Manager and FormHandler classes for Post Entity, (*9)
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']
Step 4: The controller
Your controller will contain less code because all the logic and operations will be done in your Manager and FormHandler classes, (*10)
``` php
/**
* Lists all post entities.
*
* @Route("/", name="post_index")
* @Method("GET")
*/
public function indexAction()
{
$posts = $this->get('appbundle.post_manager')->getRepository()->findAll();, (*11)
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');
}
```, (*12)
3. Example
Demo Application, (*13)
4. Suggestions
Much like every other piece of software MbxArchitectBundle
is not perfect.
Any suggestion that can improve or add features to this bundle is appreciated., (*14)
5. Reporting an issue or a feature request
Issues and feature requests are tracked in the Github issue tracker., (*15)
6. Friendly License
This bundle is available under the MIT license. See the complete license in the bundle:, (*16)
Resources/meta/LICENSE
You are free to use, modify and distribute this software, as long as the copyright header is left intact (specifically the comment block which starts with /*)!, (*17)