Download the PHP package wjb/rewire-bundle without Composer
On this page you can find all versions of the php package wjb/rewire-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package rewire-bundle
Use objects to generate routes
Let's say you have a blog application with route like this:
then your templates will have this:
For this simple case, ParamConverter is automatically applied.
The problem
Now imagine you want to change it to use {slug}
instead of {id}
in your URL. Updating controller is simple, but updating all the templates is not so.
The solution
This bundle acts as ReverseParamConverter. Check this example:
and the template:
Now instead of new_post.id
you can use object itself and this bundle will use it to generate {id}
needed for route.
So if you want to slug instead of id in your route, just update your controller:
and that's all, template doesn't require any changes.
Note
If you install this bundle, you don't have to change all your templates immediatelly. So both these combinations would still work:
Complex routes
The above example is pretty simple so let's try something more complex. For this, imagine we wanted to add {category_slug}
to URL for better SEO.
The old way:
For this fictional blog application, generated route would be something like /conspiracy-theories/why-they-suck
. You can see why updating templates can be a problem.
With this bundle:
your template doesn't require any changes, it would remain the same:
Usage
The @RewireParams annotation has 2 properties:
- requires: these are names of your parameters.
post
is used in all examples. - rewire: a set of key=>value pairs where key is the name used in your @Route annotation and value is PropertyAccess rule for reading objects.
Perfomance
While there is perfomance hit, I didn't notice any on page with 106 routes (largest I have). It is probably too small to spot it but more testing is needed.
Program works by caching @RewireParams annotations and later using it in Router decorator class. Given this bundle is built in a day, I am sure improvements can be made.
Installation
composer require wjb/rewire-bundle
If you are on Symfony4, you don't have to do anything else. For Symfony3, enable the bundle in your AppKernel:
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
...
new wjb\RewireBundle\wjbRewireBundle(),
...
All versions of rewire-bundle with dependencies
symfony/property-access Version ~3.2|~4.0
doctrine/annotations Version ^1.0