generateAbsoluteUrl example


    private UrlHelper $urlHelper;

    public function __construct(UrlHelper $urlHelper)
    {
        $this->urlHelper = $urlHelper;
    }

    public function getFunctions(): array
    {
        return [
            new TwigFunction('absolute_url', $this->generateAbsoluteUrl(...)),
            new TwigFunction('relative_path', $this->generateRelativePath(...)),
        ];
    }

    /** * Returns the absolute URL for the given absolute or relative path. * * This method returns the path unchanged if no request is available. * * @see Request::getUriForPath() */
    
class HttpFoundationExtensionTest extends TestCase
{
    /** * @dataProvider getGenerateAbsoluteUrlData */
    public function testGenerateAbsoluteUrl($expected$path$pathinfo)
    {
        $stack = new RequestStack();
        $stack->push(Request::create($pathinfo));
        $extension = new HttpFoundationExtension(new UrlHelper($stack));

        $this->assertEquals($expected$extension->generateAbsoluteUrl($path));
    }

    public static function getGenerateAbsoluteUrlData()
    {
        return [
            ['http://localhost/foo.png', '/foo.png', '/foo/bar.html'],
            ['http://localhost/foo/foo.png', 'foo.png', '/foo/bar.html'],
            ['http://localhost/foo/foo.png', 'foo.png', '/foo/bar'],
            ['http://localhost/foo/bar/foo.png', 'foo.png', '/foo/bar/'],

            ['http://example.com/baz', 'http://example.com/baz', '/'],
            [
Home | Imprint | This part of the site doesn't use cookies.