Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 541 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Symfony2 / Twig : Generate Alternate Absolute URL Path for use with CDN?

#1
This [stackoverflow solution](

[To see links please register here]

) almost answers my question. But I want to generate CDN urls in `Twig` to more than just resources. I'd like to generate them for dynamic content. So I don't think an Assetic tweak is the right area to be looking.

For now, I can set CDN_url in `parameters.ini` and use that in my urls. My code would look like this : `{{CDN_Url}}{{url('route',{'param1':'value'}}` .. Code maintenance is one major reason I don't like this option. Then you could do things like check the route for `/cdn/` to generate the CDN url and everything else to be based on the domain. I wouldn't have to run around changing a lot of variables. AND if I wanted to stop the CDN url from generating. I could just modify the route. So there's a lot of benefits to figuring out if there's a clean solution to this.

If, there's not a solution already - how would I start to extend the `{{ url() }}` functionality so that I could use it like `path` and `url`.
Reply

#2
I don't know about the CDN stuff but as far as extending the url function, take a look at:

Symfony\Bridge\Twig\Extension\RoutingExtension

public function __construct(UrlGeneratorInterface $generator)
{
$this->generator = $generator;
}
public function getFunctions()
{
return array(
'url' => new \Twig_Function_Method($this, 'getUrl'),
'path' => new \Twig_Function_Method($this, 'getPath'),
);
}
public function getPath($name, $parameters = array())
{
return $this->generator->generate($name, $parameters, false);
}

public function getUrl($name, $parameters = array())
{
return $this->generator->generate($name, $parameters, true);
}

So you could get at getUrl there or you could insert you own UrlGenerator. Not sure which would be easier in your specific case. Probably making your own UrlGenerator.
Reply

#3
You can do it with the help of the question you linked by following way

{{ asset(path('route',{'param1':'value'})) }}

If you need to handle multiple CDN domains you can do it by following way

In `app/config.yml`


# app/config.yml
#....
templating:
engines: ['twig']
packages:
cdn1:
base_urls: ["http://cdn1.domain.com"]
cdn2:
base_urls: ["http://cdn2.domain.com"]

And then in your twig template file

{{ asset('path/of/file', 'cdn1')
OR

{{ asset('path/of/file', 'cdn2')
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through