symfony-ts-router
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

Description

This is a Typescript port of js router from FOSJsRoutingBundle, that is used to expose Symfony app routing in your JavaScript code.

Following the original package readme:

That means you'll be able to generate URL with given parameters like you can do with the Router component provided in the Symfony2 core.

The problem is that there is no npm package for that router, so the only way to obtain it is to install FOSJsRoutingBundle with composer, which can be painful in pure-frontend repos, for example. So that is the missing one package.

Btw, there is other packages like this as well:

lots of them

PR`s are greatly welcomed.

Why should you use it

  • Original code rewritten in typescript, so its typings are always inlined with the code
  • All original test cases are here, and they are still green
  • There is es5 UMD modules, source maps and d.ts declarations

Installation

With npm:

npm install symfony-ts-router --save

Usage

With script tags

routes should be dumped in js format (which is default), and you should use symfony-ts-router/dist/shim.js

in twig:

<script src="{{ asset('node_modules/symfony-ts-router/dist/shim.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>

or plain html:

<script src="/node_modules/symfony-ts-router/dist/shim.js"></script>
<script src="/js/fos_js_routes.js"></script>

In javascript

You can create router instance yourself and consume json routes

import {Router} from 'symfony-ts-router';
import routes from '../../web/js/fos_js_routes.json';
 
const router = new Router();
 
router.setRoutingData(routes);
router.generate('homepage');

You can use singleton version provided in the shim package:

import {Routing} from 'symfony-ts-router/dist/shim';
import routes from '../../web/js/fos_js_routes.json';
 
Routing.setData(routes);
Routing.generate('homepage');

You can use js format routes (with default callback, which is: fos.Router.setData)

import {Routing} from 'symfony-ts-router/dist/shim';
import '../../web/js/fos_js_routes.js';
 
Routing.generate('homepage');

Dependents (0)

Package Sidebar

Install

npm i symfony-ts-router

Weekly Downloads

1,317

Version

1.0.6

License

MIT

Unpacked Size

77.2 kB

Total Files

18

Last publish

Collaborators

  • laland