Download the PHP package mach/silex-rest without Composer
On this page you can find all versions of the php package mach/silex-rest. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mach/silex-rest
More information about mach/silex-rest
Files in mach/silex-rest
Package silex-rest
Short Description RestApplicationServiceProvider for Silex
License MIT
Homepage http://github.com/tiraeth/silex-rest
Informations about the package silex-rest
RestApplicationServiceProvider
RestApplicationServiceProvider for Silex gives developers ability to rapidly create REST applications.
Few words about the conversions are needed. This provider gives you a service to build RESTful routes faster and more consistently across the whole application. While you can create , , , , and actions, not all can be used with individual items or collections. The endpoint should be defined as a plural name of the item type, e.g. (but you are not limited by the provider and can use whatever convention you like).
- can be used to fetch the whole collection under or a single object under ,
- can be used to insert new item under ,
- can be used to fully update existing item under ,
- can be used to partially update existing item under ,
- can be used to remove existing item under .
Unfortunately there is no option to automatically create custom route for action, e.g. , but you can still do it manually by adding such route to .
The library requires you to have enabled because I recommend you to use a class for a resource's controller. This way you can keep your application well-organized and reuse the controllers in Symfony2, for instance. And if you decide to keep your controllers with Silex only, you can use which implements to fastly throw 404 in case of your will to hide some actions from users, and delegates method calls to (passed by constructor) if needed.
Installation w/ Composer
- Add requirement using CLI: .
- Update the requirement .
Alternatively you can add the requirement manually:
Usage
Registering the provider
There is also a trait available for more cleaner usage.
Basic usage
Registering resource
Creating collection actions
Creating item actions
Adding converter
You can use converters with item actions. This can help you convert between to the whole entity for example.
Adding constraint check to item ID
To check if the provided ID is e.g. an integer, you can add global resource assertion, just like in regular Silex route.
Adding and middlewares
Middlewares are action-specific and you can't (sorry) define a global middleware for all actions.
Controller class usage
You can use an existing service controller or register one automatically when passing an object to .
Using existing controller service
Your class should define , , , , , and methods. Later I will show you how to change method names across the application.
Creating controller service on fly
What more can you do with RestApplicationServiceProvider?
Subresources
You can use subroutes for pairing resources together. For example, if you have a relation between Users and Notes (one-to-many) and use user friendly URLs (), you can create a subresource.
The variable to which note ID will be bound to will be . If you create a subresource to the next ID will be (see the convention?). But you can change it. Simply, pass the name as third parameter of subresource:
Overriding default method names for controller classes
While registering the provider, pass variables to the container.
You can also change a chosen method on-fly between and calls. To change item method, call which will affect further (sub)resource creations.
License
RestApplicationServiceProvider is licensed under the MIT license.