This is a copy because the original repo has gone! The repository is therefore archived & readonly
If you use JavaScript-Frameworks like Backbone.js, Ext JS and others there is the need to generate URLs in JavaScript. This plugin enables you to use the Symfony2 routing definitions to generate the URLs.
First, grab the RtxLabsJsRoutingBundle. There are two different ways to do this:
Add the following lines to your deps
file and then run php bin/vendors install
:
[RtxLabsJsRoutingBundle]
git=https://github.com/RtxLabs/JsRoutingBundle.git
target=bundles/RtxLabs/Bundle/JsRoutingBundle
Run the following commands to bring in the needed libraries as submodules.
git submodule add https://github.com/RtxLabs/JsRoutingBundle.git vendor/bundles/RtxLabs/Bundle/JsRoutingBundle
Add the following namespace entry to the registerNamespaces
call
in your autoloader:
<?php
// app/autoload.php
$loader->registerNamespaces(array(
// ...
'RtxLabs' => __DIR__.'/../vendor/bundles',
// ...
));
To start using the bundle, register it in your Kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new RtxLabs\JsRoutingBundle\RtxLabsJsRoutingBundle(),
);
// ...
)
The Bundle brings a Controller class that is used to generate a JavaScript-Object with routes on the fly. You need to add this Controller to your twig template.
<script src="{{ asset('routes/routes.js') }}"></script>
Please note that you can't use Assetic to load this resource because it only looks like a static resource. It is a dynamic one generated via a Controller!
You can dump these routes to a static JavaScript files too. This is a good way to improve performance on a production system. For that you can call the rtx:rout:dump command that writes all routes to a JS-File.
php app/console rtx:route:dump web
Before you can use the routes you need to add another JS-File.
{% javascripts '@RtxLabsJsRoutingBundle/Resources/public/js/router.js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
Afterwards you will be able to use the Symfony routes in JavaScript. You can even replace the route-placeholders with values from a JS-Object
// Route sales_dashboard = /sales/dashboard
location.href = RtxLabs.router.url('edit_user');
// Route edit_user = /user/edit/{id}
location.href = RtxLabs.router.url('edit_user', {'id': 13});
All errors happening during the Routing will logged to the Console-Object (Chrome, Firebug...)
- Add caching