knplabs / rad-url-generation
Simply auto-complete needed route parameters with existing ones.
Installs: 5 912
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 26
Forks: 4
Open Issues: 0
Requires
- php: ~7.0
- symfony/config: ~2.8||~3.0
- symfony/dependency-injection: ~2.8||~3.0
- symfony/http-kernel: ~2.8||~3.0
- symfony/routing: ~2.8||~3.0
Requires (Dev)
- knplabs/phpspec-welldone-extension: dev-master
- phpspec/phpspec: ~2.4
README
Unfortunately we decided to not maintain this project anymore (see why). If you want to mark another package as a replacement for this one please send an email to hello@knplabs.com.
Rapid Application Development : Url Generation
Simply auto-complete needed route parameters with existing ones.
Official maintainers:
Installation
composer require knplabs/rad-url-generation ~2.0
class AppKernel { function registerBundles() { $bundles = array( //... new Knp\Rad\UrlGeneration\Bundle\UrlGenerationBundle(), //... ); //... return $bundles; } }
Usages
Just continue to use former url generation, nothing changes concerning the implementation. The only change is you don't repeat current route parameters anymore.
Example 1
Current route:
app_products_show: path: /shops/{shop}/products/{products}/
My current url is /shops/12/products/345/
. I want to build again the same url. Should I repeat route parameters if it already is my current route?
Nope.
$router->generate('app_products_show'); // Returns /shops/12/products/345/ $router->generate('app_products_show', ['product' => 122]); // Returns /shops/12/products/122/ $router->generate('app_products_show', ['shop' => 1]); // Returns /shops/1/products/345/ $router->generate('app_products_show', ['shop' => 1, 'product' => 122]); // Returns /shops/1/products/122/
Example 2
Current route:
app_products_show: path: /shops/{shop}/products/{products}/
Current URL: /shops/1/products/122/
I want to build this URL
app_variant_show: path: /shops/{shop}/products/{products}/variants/{variant}/
I could execute:
$router->generate('app_variant_show', ['shop' => 1, 'product' => 122, 'variant' => 23]); // Returns /shops/1/products/122/variants/23/
But why should I repeat already existing parameters ?
$router->generate('app_variant_show', ['variant' => 23]); // Returns /shops/1/products/122/variants/23/
Works with
- Router service :
$container->get('router')->generate('app_products_show')
. - Controller shortcuts:
$this->generateUrl('app_products_show')
and$this->redirectToRoute('app_products_show')
. - Twig functions:
path('app_products_show')
orurl('app_products_show')
. - Everything else using the Symfony router.