knplabs / controller-behaviors
Trait based Symfony2 controllers
Installs: 729
Dependents: 0
Suggesters: 0
Security: 0
Stars: 36
Watchers: 36
Forks: 1
Open Issues: 1
Requires
- php: >=5.4.0
- symfony/http-foundation: ~2.1
Requires (Dev)
- doctrine/doctrine-bundle: *
- doctrine/mongodb-odm-bundle: *
- doctrine/orm: *
- knplabs/knp-components: dev-master
- phpspec/phpspec2: 1.0.*
- propel/propel1: dev-master
- symfony/form: *
- symfony/framework-bundle: 2.1.*
- symfony/validator: *
This package is not auto-updated.
Last update: 2022-02-01 12:21:46 UTC
README
This php 5.4+ library is a collection of traits that adds behaviors to Symfony2 controllers.
It currently handles:
- crudable (Doctrine2 ORM and ODM, Propel)
- filterable
- paginable
Usage
All you have to do is to define a Controller and use some traits.
crudable:
Crudable trait is an abstract trait used internally by ORMBehavior and ODMBehavior.
- To use Doctrine ORM persistence in your CRUD, just use ORMBehavior like below.
- To use Propel ORM persistence in your CRUD, just use PropelBehavior.
<?php namespace Acme\DemoBundle\Controller; use Knp\ControllerBehaviors\Crudable\Doctrine\ORMBehavior; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class MemberController extends Controller { // make aliases of actions to make them FOSRestBundle compliant use ORMBehavior { ORMBehavior::getListResponse as getMembersAction; ORMBehavior::getShowResponse as getMemberAction; ORMBehavior::getNewResponse as newMembersAction; ORMBehavior::getEditResponse as editMemberAction; ORMBehavior::getCreateResponse as postMembersAction; ORMBehavior::getUpdateResponse as putMemberAction; ORMBehavior::getDeleteResponse as deleteMemberAction; } }
Form
The create, edit and update actions of Crudable
will search for a <Object>Type
in the Form
folder of the bundle.
In the examples below, it would be in src/Acme/DemoBundle/Form/MemberType
.
To modify this behavior, just override the default implementation of the trait, like this:
<?php protected function createNewForm($object) { return $this->createForm('my_super_type_id', $object, ['some_option' => true]); }
Templates
Templates are also searched using conventions. By default it will search in the Resources\views/<ControllerName>
folder of your bundle.
<ControllerName>
here can be contain a subfolder (think of an Admin
subfolder for example).
To modify this behavior, just override the default implementation of the trait, like this:
<?php protected function getViewsPathPrefix() { return '::'; }
Filterable
Filterable behavior is a simple trait that stores and retrieves some informations for a given controller, like filter form data.
Once you posted data to postFilterMembersAction
, you can retrieve it later by using the getFilters
method.
It also provides a way to handle a filter form, whose type yould be defined in src/Acme/DemoBundle/Form/MemberFilterType
in this example.
<?php use Knp\ControllerBehaviors\FilterableBehavior; class MemberController extends Controller { // make aliases of actions to make them FOSRestBundle compliant use FilterableBehavior { FilterableBehavior::getFilterResponse as postFilterMembersAction; } }
In order to make this filter form visible in the view, you can override default view parameters handling:
<?php protected function getListViewParameters(array $parameters) { return array_merge([ 'filterForm' => $this->createFilterForm()->createView(), ], $parameters); }
Paginable
Paginable behavior is a simple trait that uses Knp paginator to paginate a resultset.
<?php use Knp\ControllerBehaviors\Paginatable\KnpPaginatorBehavior; class MemberController extends Controller { use KnpPaginatorBehavior; public function getObjectsToList() { return $this->paginateQueryBuilder( $this->getObjectRepository()->getJoinAllFilteredQB($this->getFilters()) // returns an ORM Query Builder ); } }