Commit 5cc54a88 authored by Igor Wiedler's avatar Igor Wiedler

[UrlGeneratorExtension] service for generating URLs for routes

Usage example:

    $app->register(new UrlGeneratorExtension());

    $app->get('/hello/:name', function($name) {
        return "Hello $name!";
    })
    ->bind('hello');

    $app->get('/go', function() use ($app) {
        $url = $app['url_generator']->generate('hello', array('name' => 'Fabien'));
        return $app->redirect($url);
    });

Thanks to @choffmeister.
parent 87929817
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Extension;
use Silex\Application;
use Silex\ExtensionInterface;
use Symfony\Component\Routing\Generator\UrlGenerator;
class UrlGeneratorExtension implements ExtensionInterface
{
public function register(Application $app)
{
$app['url_generator'] = $app->share(function() use ($app) {
$app->flush();
return new UrlGenerator($app['routes'], array(
'base_url' => $app['request']->getBaseUrl(),
'method' => $app['request']->getMethod(),
'host' => $app['request']->getHost(),
'port' => $app['request']->getPort(),
'is_secure' => $app['request']->isSecure(),
));
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment