Download the PHP package mmoreram/controller-extra-bundle without Composer
On this page you can find all versions of the php package mmoreram/controller-extra-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mmoreram/controller-extra-bundle
More information about mmoreram/controller-extra-bundle
Files in mmoreram/controller-extra-bundle
Package controller-extra-bundle
Short Description Some specific controller annotations
License MIT
Homepage https://github.com/mmoreram/ControllerExtraBundle
Informations about the package controller-extra-bundle
ControllerExtra for Symfony2
This bundle provides a collection of annotations for Symfony2 Controllers, designed to streamline the creation of certain objects and enable smaller and more concise actions.
Table of contents
- Reference
- Entity Provider
- By namespace
- By doctrine shortcut
- By parameter
- Controller Annotations
- @CreatePaginator
- Paginator Entity
- Paginator Page
- Paginator Limit
- Paginator OrderBy
- Paginator Wheres
- Paginator Left Joins
- Paginator Inner Joins
- Paginator Not Nulls
- Paginator Attributes
- Paginator Example
- Pagerfanta Add-on
- KNPPaginator Add-on
- @LoadEntity
- Entity Mapping
- Entity Mapping fallback
- Entity Repository
- Entity Factory
- @CreateForm
- @Flush
- @ToJsonResponse
- @Log
- @Get
- @Post
- @CreatePaginator
- Custom annotations
- Annotation
- Resolver
- Definition
- Registration
Reference
By default, all annotations are loaded, but any individual annotation can be
completely disabled by setting to false active
parameter.
Default values are:
ResolverEventListener is subscribed to
kernel.controller
event with priority -8. This element can be configured and customized withresolver_priority
config value. If you need to get ParamConverter entities, make sure that this value is lower than 0. The reason is that this listener must be executed always after ParamConverter one.
Entity provider
In some annotations, you can define an entity by several ways. This chapter is about how you can define them.
By namespace
You can define an entity using its namespace. A simple new new()
be performed.
By doctrine shortcut
You can define an entity using Doctrine shortcut notations. With this format
you should ensure that your Entities follow Symfony Bundle standards and your
entities are placed under Entity/
folder.
By parameter
You can define an entity using a simple config parameter. Some projects
use parameters to define all entity namespaces (To allow overriding). If you
define the entity with a parameter, this bundle will try to instance it
with a simple new()
accessing directly to the container ParametersBag.
Controller annotations
This bundle provide a reduced but useful set of annotations for your controller actions.
@CreatePaginator
Creates a Doctrine Paginator object, given a request and a configuration. This
annotation just injects into de controller a new
Doctrine\ORM\Tools\Pagination\Pagination
instance ready to be iterated.
You can enable/disable this bundle by overriding active
flag in configuration file
config.yml
By default, if
name
option is not set, the generated object will be placed in a parameter named$paginator
. This behaviour can be configured usingdefault_name
in configuration.
This annotation can be configured with these sections
Paginator Entity
To create a new Pagination object you need to refer to an existing Entity. You can check all available formats you can define it just reading the Entity Provider section.
Paginator page
You need to specify Paginator annotation the page to fetch. By default, if none
is specified, this bundle will use the default one defined in configuration. You
can override in config.yml
You can refer to an existing Request attribute using ~value~
format, to any
$_GET
element by using format ?field?
or to any $_POST
by using format
#field#
You can choose between Master Request or Current Request accessing to its attributes, by configuring the request value of the configuration.
or you can hardcode the page to use.
Paginator limit
You need to specify Paginator annotation the limit to fetch. By default, if none
is specified, this bundle will use the default one defined in configuration. You
can override in config.yml
You can refer to an existing Request attribute using ~value~
format, to any
$_GET
element by using format ?field?
or to any $_POST
by using format
#field#
or you can hardcode the page to use.
Paginator OrderBy
You can order your Pagination just defining the fields you want to orderBy and
the desired direction. The orderBy
section must be defined as an array of
arrays, and each array should contain these positions:
- First position: Entity alias (Principal object is set as
x
) - Second position: Entity field
- Third position: Direction
- Fourth position: Custom direction map (optional)
With the third and fourth value you can define a map where to match your own direction nomenclature with DQL one. DQL nomenclature just accept ASC for Ascendant and DESC for Descendant.
This is very useful when you need to match a url format with the DQL one. You
can refer to an existing Request attribute using ~value~
format, to any
$_GET
element by using format ?field?
or to any $_POST
by using format
#field#
The order of the definitions will alter the order of the DQL query.
Paginator Wheres
You can define some where statements in your Paginator. The wheres
section
must be defined as an array of arrays, and each array should contain these
positions:
- First position: Entity alias (Principal object is set as
x
) - Second position: Entity field
- Third position: Operator =, <=, >, LIKE...
- Fourth position: Value to compare with
- Fifth position: Is a filter. By default, false
You can refer to an existing Request attribute using ~value~
format, to any
$_GET
element by using format ?field?
or to any $_POST
by using format
#field#
You can use as well this feature for optional filtering by setting the last
position to true
. In that case, if the filter value is not found, such line
will be ignored.
Paginator Not Nulls
You can also define some fields to not null. Is same as wheres
section, but
specific for NULL assignments. The notNulls
section must be defined as an array
of arrays, and each array should contain these positions:
- First position: Object (Principal object is set as
x
) - Second position: Field
Paginator Left Join
You can do some left joins in this section. The leftJoins
section must be
defined as an array of array, where each array can have these fields:
- First position: Entity alias (Principal object is set as
x
) - Second position: Entity relation (Address)
- Third position: Relation identifier (a)
- Fourth position: If true, this relation is added in select group. Otherwise, wont be loaded until its request (optional)
Paginator Inner Join
You can do some inner joins in this section. The innerJoins
section must be
defined as an array of array, where each array can have these fields:
- First position: Entity alias (x)
- Second position: Entity relation (Address)
- Third position: Relation identifier (a)
- Fourth position: If true, this relation is added in select group. Otherwise, wont be loaded until its request (optional)
Paginator Attributes
A nice feature of this annotation is that you can also inject into your
controller a Mmoreram\ControllerExtraBundle\ValueObject\PaginatorAttributes
instance with some interesting information about your pagination.
- currentPage : Current page fetched
- totalElements : Total elements given your criteria. If none criteria is defined in your configuration, this value will show all elements of a certain entity.
- totalPages : Total pages you can fetch given a criteria.
- limitPerPage: Maximum number of elements in each page.
To inject this object you need to define the "attributes" annotation field with the method parameter name.
Paginator Example
This is a completed example and its DQL resolution
The DQL generated by this annotation is
PagerFanta Add-on
This annotation can create a PagerFanta instance if you need it. You only have to define your parameter as such, and the annotation resolver will wrap your paginator with a Pagerfanta object instance.
KNPPaginator Add-on
This annotation can create a KNPPaginator instance if you need it. You only have to define your parameter as such, and the annotation resolver will wrap your paginator with a KNPPaginator object instance.
@LoadEntity
Loads an entity from your database, or creates a new one.
By default, if
name
option is not set, the generated object will be placed in a parameter named$entity
. This behaviour can be configured usingdefault_name
in configuration.
You can also use setters in Entity annotation. It means that you can simply call entity setters using Request attributes.
When User
instance is built, method setAddress
is called using as parameter
the new Address
instance.
New entities are just created with a simple new()
, so they are not persisted.
By default, they will be persisted using configured manager, but you can disable
this feature using persist
option.
Entity Mapping
When you define a new Entity annotation, you can also request the mapped entity given a map. It means that if a map is defined, this bundle will try to request the mapped instance satisfying it.
The keys of the map represent the names of the mapped fields and the values
represent their desired values. Remember than you can refer to any Request
attribute by using format ~field~
, to any $_GET
element by using format
?field?
or to any $_POST
by using format #field#
In this case, you will try to get the mapped instance of User with passed id. If some mapping is defined and any entity is found, a new EntityNotFoundException` is thrown.
Entity Mapping Fallback
So what if one ore more than one mapping references are not found? For example, you're trying to map the {id} parameter from your route, but this parameter is not even defined. Whan happens here? Well, you can assume then that you want to pass a new entity instance by using the mappingFallback.
By default, if
mapping_fallback
option is not set, the used value will be the parameterdefault_mapping_fallback
defined in configuration. By default this value isfalse
Don't confuse with the scenario where you're looking for an entity in your database, all mapping references have been resolved, and the entity is not found. In that case, a common "EntityNotFound" exception will be thrown by Doctrine.
Lets see an example. Because we have enabled the mappingFallback, and because the mapping definition does not match the assigned route, we will return a new empty User entity.
Entity Repository
By default, the Doctrine entity manager provides the right repository per each entity (not the default one, but the right specific one). Although, you can define a custom repository to be used in your annotation by using the repository configuration.
By default, the method findOneBy will always be used, unless you define another one.
Entity Factory
When the annotation considers that a new entity must be created, because no mapping information has been provided, or because the mapping fallback has been activated, by default a new instance will be created by using the namespace value.
This configuration block has three positions
- class - factory class
- method - Method to use when retrieving the object
- static - Method is static
You can define the factory with a simple namespace
If you want to define your Factory as a service, with the possibility of overriding namespace, you can simply define service name. All other options have the same behaviour.
If you do not define the method
, default one will be used. You can
override this default value by defining new one in your config.yml
. Same with
static
value
@CreateForm
Provides form injection in your controller actions. This annotation only needs a name to be defined in, where you must define namespace where your form is placed.
By default, if
name
option is not set, the generated object will be placed in a parameter named$form
. This behaviour can be configured usingdefault_name
in configuration.
You can not just define your Type location using the namespace, in which case a new AbstractType element will be created. but you can also define it using service alias, in which case this bundle will return an instance using Symfony DI.
This annotation allows you to not only create an instance of FormType, but also allows you to inject a Form object or a FormView object
To inject a Form object you only need to cast method value as such.
You can also, using [SensioFrameworkExtraBundle][1]'s [ParamConverter][2],
create a Form object with an previously created entity. you can define this
entity using entity
parameter.
To handle current request, you can set handleRequest
to true. By default
this value is set to false
You can also add as a method parameter if the form is valid, using validate
setting. Annotation will place result of $form->isValid()
in specified method
argument.
To inject a FormView object you only need to cast method variable as such.
@Flush
Flush annotation allows you to flush entityManager at the end of request using kernel.response event
If not otherwise specified, default Doctrine Manager will be flushed with this
annotation. You can overwrite default Manager in your config.yml
file.
You can also override this value in every single Flush Annotation
instance defining manager
value
If you want to change default manager in all annotation instances, you should
override bundle parameter in your config.yml
file.
If any parameter is set, annotation will flush all. If you only need to flush one or many entities, you can define explicitly which entity must be flushed.
You can also define a set of entities to flush
If multiple @Mmoreram\Flush are defined in same action, last instance will overwrite previous. Anyway just one instance should be defined.
@ToJsonResponse
JsonResponse annotation allows you to create a
Symfony\Component\HttpFoundation\JsonResponse
object, given a simple
controller return value.
By default, JsonResponse is created using default status
and headers
defined
in bundle parameters. You can overwrite them in your config.yml
file.
You can also overwrite these values in each @JsonResponse
annotation.
If an Exception is returned the response status is set by default to 500 and the Exception message is returned as response.
STATUS 500 Internal server error
In case we use a HttpExceptionInterface the use the exception status code as status code. In case we launch this exception
We'll receive this response
STATUS 404 Not Found
If the exception is being launched on an annotation (e.g. Entity annotation) remember to add the JsonResponse annotation at the beginning or at least before any annotation that could cause an exception.
If multiple @Mmoreram\JsonResponse are defined in same action, last instance will overwrite previous. Anyway just one instance should be defined.
@Log
Log annotation allows you to log any plain message before or after controller action execution
You can define the level of the message. You can define default one if none is
specified overriding it in your config.yml
file.
Every Annotation instance can overwrite this value using level
field.
Several levels can be used, as defined in [Psr\Log\LoggerInterface][6] interface
- @Mmoreram\Log::LVL_EMERG
- @Mmoreram\Log::LVL_CRIT
- @Mmoreram\Log::LVL_ERR
- @Mmoreram\Log::LVL_WARN
- @Mmoreram\Log::LVL_NOTICE
- @Mmoreram\Log::LVL_INFO
- @Mmoreram\Log::LVL_DEBUG
- @Mmoreram\Log::LVL_LOG
You can also define the execution of the log. You can define default one if
none is specified overriding it in your config.yml
file.
Every Annotation instance can overwrite this value using level
field.
Several executions can be used,
- @Mmoreram\Log::EXEC_PRE - Logged before controller execution
- @Mmoreram\Log::EXEC_POST - Logged after controller execution
- @Mmoreram\Log::EXEC_BOTH - Logged both
@Get
The Get annotation allows you to get any parameter from the request query string.
For a GET
request like:
You can can simply get the foo
var using the GET
annotation
You can also customize the var name and the default value in case the var is not sent on the query string.
For a GET
request like:
And this annotation
@Post
The Post annotation allows you to get any parameter from the post request body.
For a POST
request like:
You can can simply get the foo
var using the POST
annotation
You can also customize the var name and the default value in case the var is not sent on the query string.
For a POST
request like:
And this annotation
Custom annotations
Using this bundle you can now create, in a very easy way, your own controller annotation.
Annotation
The annotation object. You need to define the fields your custom annotation
will contain. Must extends Mmoreram\ControllerExtraBundle\Annotation\Annotation
abstract class.
Resolver
Once you have defined your own annotation, you have to resolve how this
annotation works in a controller. You can manage this using a Resolver. Must
extend Mmoreram\ControllerExtraBundle\Resolver\AnnotationResolver;
abstract
class.
This class will be defined as a service, so this method is computed just
before executing current controller. You can also subscribe to some kernel
events and do whatever you need to do ( You can check
Mmoreram\ControllerExtraBundle\Resolver\LogAnnotationResolver
for some
examples.
Definition
Once Resolver is done, we need to define our service as an Annotation
Resolver. We will use a custom tag
.
Registration
We need to register our annotation inside our application. We can just do it in
the boot()
method of bundle.php
file.
Et voilà! We can now use our custom Annotation in our project controllers.
All versions of controller-extra-bundle with dependencies
doctrine/common Version ^2.5
symfony/config Version ^3.2 || ^4.0
symfony/http-kernel Version ^3.2 || ^4.0
symfony/http-foundation Version ^3.2 || ^4.0
symfony/dependency-injection Version ^3.2 || ^4.0
mmoreram/base-bundle Version ^1.0.3
mmoreram/symfony-bundle-dependencies Version ^2.0