your programing

나뭇 가지 함수에서 절대 경로를 사용하는 방법

lovepro 2020. 12. 31. 23:10
반응형

나뭇 가지 함수에서 절대 경로를 사용하는 방법


Symfony2 (2.2)에 응용 프로그램이 있습니다. 메일을 보내려고 할 때 경로에 문제가 있습니다. 경로는 모두 상대 경로이며 분명히 이메일 내부에서 작동하지 않습니다.

내가 사용하는 경로를 렌더링하기 위해 :

<a href="{{ path('route_name', {'param' : value}) }}">A link</a>

및 자산 :

<img src="{{ asset('bundle/myname/img/image.gif') }}" alt="Title"/>

이전 예제는 잘 작동하지만 경로는 상대적이므로 도메인을 추가해야합니다. 다음과 같이 할 수 있습니다.

<a href="http://domain.com{{ path('route_name', {'param' => param1}) }}">A link</a>

그러나 도메인이 다르기 때문에 이것은 내 문제에 대한 최상의 솔루션이 아닙니다.

최신 정보

url기능 이있는 경로에 대한 솔루션을 찾았 지만 여전히 자산에 대한 솔루션이 필요합니다.


Symfony 2.7 이상

여기 에서이 답변을 참조 하십시오.

첫 번째 작업 옵션

{{ app.request.scheme ~'://' ~ app.request.httpHost ~ asset('bundles/acmedemo/images/search.png') }}

두 번째 작업 옵션-선호

깨끗한 새 Symfony 사본으로 빠르게 테스트했습니다. 체계와 httpHost를 결합하는 또 다른 옵션 이 있습니다 .

{{ app.request.getSchemeAndHttpHost() ~ asset('bundles/acmedemo/images/search.png') }}
{# outputs #}
{# http://localhost/Symfony/web/bundles/acmedemo/css/demo.css  #}

Symfony 2.7에는 절대 URL을 생성하는 데 사용할 수 있는 새로운 absolute_url 이 있습니다. http://symfony.com/blog/new-in-symfony-2-7-the-new-asset-component#template-function-changes

두 경우 모두 또는 경로 문자열에서 작동합니다.

<a href="{{ absolute_url(path('route_name', {'param' : value})) }}">A link</a>

및 자산 :

<img src="{{ absolute_url(asset('bundle/myname/img/image.gif')) }}" alt="Title"/>

또는 모든 문자열 경로

<img src="{{ absolute_url('my/absolute/path') }}" alt="Title"/>

이러한 트리 케이스에서는 다음과 같은 절대 URL로 끝납니다.

http://www.example.com/my/absolute/path

에서 Symfony2 문서 : 자산에 대한 절대 URL은 심포니 2.5에 소개되었다.

자산에 대한 절대 URL이 필요한 경우 세 번째 인수 (또는 절대 인수)를 true로 설정할 수 있습니다.

예:

<img src="{{ asset('images/logo.png', absolute=true) }}" alt="Symfony!" />

Daniel의 대답 은 현재 잘 작동하는 것 같지만 twig의 asset기능을 사용하여 절대 URL을 생성 하는 것은 이제 더 이상 사용되지 않습니다.

지원 중단됨-Twig asset () 함수로 절대 URL 생성은 2.7에서 더 이상 사용되지 않으며 3.0에서 제거됩니다. 대신 absolute_url ()을 사용하십시오.

공식 발표는 다음과 같습니다. http://symfony.com/blog/new-in-symfony-2-7-the-new-asset-component#template-function-changes

absolute_urltwig 기능 을 사용해야합니다 .

{# Symfony 2.6 #}
{{ asset('logo.png', absolute = true) }}

{# Symfony 2.7 #}
{{ absolute_url(asset('logo.png')) }}

path기능 과 함께 작동한다는 점은 흥미 롭습니다 .

{{ absolute_url(path('index')) }}

assets_base_urls구성 을 사용하고 싶을 것입니다 .

framework:
    templating:
        assets_base_urls:
            http:   [http://www.website.com]
            ssl:   [https://www.website.com]

http://symfony.com/doc/current/reference/configuration/framework.html#assets


구성은 Symfony 2.7 이후로 다릅니다 .

framework:
    # ...
    assets:
        base_urls:
            - 'http://cdn.example.com/'

It's possible to have http://test_site.com and https://production_site.com. Then hardcoding the url is a bad idea. I would suggest this:

{{app.request.scheme ~ '://' ~ app.request.host ~ asset('bundle/myname/img/image.gif')}}

The following works for me:

<img src="{{ asset('bundle/myname/img/image.gif', null, true) }}" />

I've used the following advice from the docs https://symfony.com/doc/current/console/request_context.html to get absolute urls in emails:

# config/services.yaml
parameters:
    router.request_context.host: 'example.org'
    router.request_context.scheme: 'https'

ReferenceURL : https://stackoverflow.com/questions/17049712/how-to-use-absolute-path-in-twig-functions

반응형