How to get the full url for an asset in Controller?

前端 未结 15 2091
悲&欢浪女
悲&欢浪女 2020-12-12 16:13

I need to generate some JSON content in controller and I need to get the full URL to an uploaded image situated here : /web/uploads/myimage.jpg.

How can

相关标签:
15条回答
  • 2020-12-12 16:59

    Method app.request.uriForPath() gives an absolute URL from a path. Helper asset() gives the path of an asset.

    {{ app.request.uriForPath(asset('bundles/mybundle/images/name.png')) }}
    

    It's important to have both for the case where the application is not at the root of the domain, for eg:

    http://mydomain.com/myapp/app.php
    
    0 讨论(0)
  • 2020-12-12 17:01

    This solution doesn't require adding any variable or parameter on config and is the same function that is used by Twig for {{ absolute_url() }}

    public function yourAction(AssetsHelper $assetsHelper, HttpFoundationExtension $httpExtension)
    {
        $assetsPath = $assetsHelper->getUrl('/img/test.jpg');
        $assetFullUrl = $httpExtension->generateAbsoluteUrl($assetPath);
    }
    

    The complete url of your asset will be on $assetFullUrl

    0 讨论(0)
  • 2020-12-12 17:02

    This one works for me

    $baseurl = $this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getBasePath();
    
    0 讨论(0)
  • 2020-12-12 17:03

    You can use the templating.helper.assets service.

    First define your assets base URL :

    # app/config/config.yml
    framework:
        templating:
            assets_base_url: "http://www.mywebsite.com/"
    

    Then just call the service from within your controller, command or wherever you are :

    <?php
    
    // src/Acme/Bundle/DemoBundle/Controller/DemoController.php
    
    namespace Acme\Bundle\DemoBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    
    class DemoController extends Controller
    {
        public function indexAction()
        {
            $myAssetUrl = $this
                ->get('templating.helper.assets')
                ->getUrl('bundles/acmedemo/js/main.js', $packageName = null)
            ;
    
            // $myAssetUrl is "http://www.mywebsite.com/bundles/acmedemo/js/main.js"
    
            return array();
        }
    }
    
    0 讨论(0)
  • 2020-12-12 17:03

    The best solution would be IMHO to look at Twig's implementation of asset, which you can find in:

    \Symfony\Bundle\TwigBundle\Extension\AssetsExtension
    

    in the method:

    public function getAssetUrl($path, $packageName = null, $absolute = false, $version = null)
    

    which basically uses the templating.helper.assets service similar to @iamdto's answer:

    $url = $this->container->get('templating.helper.assets')->getUrl($path, $packageName, $version);
    

    Or directly use the AssetsExtension service or class respectively.

    0 讨论(0)
  • 2020-12-12 17:04

    For CDNs, you can also inject the Packages directly into your service.

    First the asset base url in config.yml:

    framework:
        assets:
            base_url:
                - 'http://cdn.mysite.com'
    

    Then the fun part:

    namespace App\Service;
    
    use Symfony\Component\Asset\Packages;
    
    class SomeClass
    {
        private $packages;
    
        public function __construct(Packages $packages)
        {
            $this->packages = $packages;
        }
    
        public function proxyToAssetUrl(string $path): string
        {
            return $this->packages->getUrl($path);
        }
    }
    

    If you have autowiring enabled then you won't need to configure anything in your services.yml, otherwise something like:

    services:
        App\Service\SomeClass:
            class: App\Service\SomeClass
            arguments: ['@assets.packages']
    
    0 讨论(0)
提交回复
热议问题