Download the PHP package programmingarehard/resource-bundle without Composer
On this page you can find all versions of the php package programmingarehard/resource-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download programmingarehard/resource-bundle
More information about programmingarehard/resource-bundle
Files in programmingarehard/resource-bundle
Package resource-bundle
Short Description Symfony bundle that helps in developing REST APIs.
License MIT
Informations about the package resource-bundle
ResourceBundle
The ResourceBundle is an opinionated Symfony bundle to aid in developing REST APIs. It makes some architectural decisions for you, allowing you to focus more on the domain of your application. It uses as little magic as possible to make it easier to understand, debug, and extend.
Prerequisites
The ResourceBundle relies on the FOSRestBundle to handle content negotiation and RESTful decoding of request bodies. After installing the bundle, you must configure it before proceeding to use the ResourceBundle. Here is a sample configuration to get started.
Note: The ResourceBundle does not handle any sort of authentication. It is meant to be used in conjunction with something like the FOSOAuthServerBundle.
Bundle Usage
- Resources
- Resource Repositories
- Resource Managers
- Resource Forms
- Form Handlers
- Form Processors
- Resource Controllers
- Resource Routing
- Events
- Bundle Configuration Reference
- Special Notes
Resources
The ResourceBundle is centered around resources. The bundle requires resources(entities) to implement the very simple ResourceInterface. The examples assume you're using Doctrine but the bundle is ORM agnostic. First, let's create a simple resource.
Resource Repositories
Once we have a resource, it's time to create a repository for the resource by implementing the ResourceRepositoryInterface. If using Doctrine, just extend the bundled BaseResourceRepository.
Register it with the container.
Remember to update the mapping file.
Resource Managers
Just like Doctrine, persisting and deleting resources is not done by repositories. With the ResourceBundle, this is done through a ResourceManagerInterface implementation. If using Doctrine, you can use the bundled ResourceManager
. Internally it uses Doctrine's ManagerRegistry
to get the correct object manager for the resource.
Resource Forms
The bundle makes use of Symfony's form component to map incoming data to resources. Time to create a form for our Task
.
By default, the bundle attempts to find a resource's form by looking for a form with the name of the resource's class
that has been lowercased and underscored. Ie. The bundle would expect to find a form by the name of todo_list
for a MyApp/CoreBundle/Entity/TodoList
resource.
Let's register the form with the container.
Form Handlers
Now that we have a resource, repository, and form it's time to create an implementation of a FormHandlerInterface
.
Form handlers are only executed if a request was issued and the form was valid. The bundle comes with a SaveResourceFormHandler
.
It extracts the data(the resource from the form) and saves it through a ResourceManagerInterface
. Let's register a resource
form handler in the container.
Form Processors
We need to use our new handler in a form processor.
Form processors use a form handler if the form is valid and an error extractor for when it is invalid. If you do not like the default form error extractor, you can create your own by implementing the FormErrorExtractorInterface.
Resource Controllers
The glue that brings all these pieces together is the abstract ResourceController.
Let's create a concrete TaskController
.
Note: The $resourceClass
is used by the ResourceController
to find the relevant form, naming events, and serializing resources.
Tip: You might want to create your own base ResourceController
and implement ::getFormProcessor()
and ::getResourceManager()
as they will probably be the same across each of your resource controllers.
Because the ResourceController
uses symfony's security component to check basic REST permissions, we need to implement
a security voter. You can customize this to suit your application's needs. For now, we're going to allow everything.
Don't forget to register it.
Resource Routing
Once we have our TaskController
and our security voter set up, we can then RESTfully route to actions.
I highly recommend you take a peek at the ResourceController to see what's happening under the hood.
By default, the ResourceBundle uses Symfony's serializer component to serialize resources for responses. However, I recommend using the JMSSerializerBundle for more flexibility.
Events
The bundle's components are developed in a manner to make it easy to add functionality. One important piece of functionality is the ability to dispatch events. Events can be dispatched by wrapping certain components in decorators. The bundle comes with three.
Eventful Resource Manager
You can use this decorator to dispatch events during manager interactions.
By using this decorator, the following events will be dispatched:
- task.pre_save
- task.post_save
- task.pre_create
- task.post_create
- task.pre_update
- task.post_update
- task.pre_delete
- task.post_delete
It uses the ResourceEventDispatcher to dispatch these events. It uses the same class transformer as the ResourceController does when finding a resource's form. It lowercases and underscores a resource's class to use in the event name. Feel free to use the resource event dispatcher in your own code(like in your event listeners).
Eventful Form Handler
You can decorate your form handlers to dispatch pre and post handle events.
Using this decorator will dispatch the following events for the task's form handler:
- task.form.pre_handle
- task.form.post_handle
Eventful Form Processor
You can also decorate form processors to dispatch certain events throughout the form processing.
Using this decorator will dispatch the following events for the tasks's form.
- task.form.initialize
- task.form.invalid (only if the form is invalid)
- task.form.complete (only if the form is valid)
To summarize, by taking advantage of these decorators you will have access to the following events:
- task.pre_save
- task.post_save
- task.pre_create
- task.post_create
- task.pre_update
- task.post_update
- task.pre_delete
- task.post_delete
- task.form.initialize
- task.form.invalid
- task.form.pre_handle
- task.form.post_handle
- task.form.complete
Don't forget to use them in your ResourceController
s though!
Bundle Configuration Reference
This is the default bundle configuration.
The class_tranformer
is responsible for turning a resource's fully qualified class name into a name it uses when
finding a resource's form and dispatching events. It must implement TransformerInterface.
The form_error_extractor
is responsbile for getting errors from a form. It must implement FormErrorExtractorInterface
Special Notes
There are a few things to keep in mind when using this bundle.
IndexAction
You might have noticed that there is no indexAction
in the ResourceController
. This is because the bundle can't reasonably guess all of the required parameters needed to come up with a generic enough solution. For example, most likely you will only want to display the current user's resources rather than everything in a resource repository. You may also want advanced url structures, ie. /api/people/24/tasks
. The indexAction
method signature we need to accommodate that person id wildcard. Consequently, pagination and filtering are left up to you.
ResourceEvents::PRE_VIEW
This event is dispatched in the create
, show
, and update
actions in the ResourceController
. Depending on how you display related resources, you may want to ignore these events. You can use a serializer to display a resource's related resources. These resources would be fetched by calling getters on the resources themselves. Consequently, there is no place to dispatch the ResourceEvents::PRE_VIEW
events for the related resources and still keep the bundle ORM agnostic. One option is to pull in the BazingaHateoasBundle and configure links for related resources. This way you can be sure that individual resources are only ever displayed directly by the ResourceController
.
All versions of resource-bundle with dependencies
symfony/framework-bundle Version ~2.1
symfony/form Version ~2.1
symfony/security-bundle Version ~2.1
friendsofsymfony/rest-bundle Version ~1.3
doctrine/inflector Version ~1.0