Getting the base URL of a website and sending it to a branch in Symfony 2

I am making the transition from CodeIgniter to Symfony 2. Can someone please give me an example of how:

  • Get base URL (URL without specific routes)
  • Pass this variable globally to a bunch of branches so I can use it in every template.
+77
url symfony base-url
Jul 27 '11 at 7:03
source share
12 answers

Why do you need this root URL? Can't you create an absolute url?

{{ url('_demo_hello', { 'name': 'Thomas' }) }} 

This Twig code generates the full http: // URL for the _demo_hello route.

In fact, getting the base URL of the website only gets the full URL of the homepage route:

 {{ url('homepage') }} 

(homepage or whatever you name in your routing file).

+81
Jul 27 2018-11-21T00:
source share

Now it is available for free in branch templates (tested on sf2 version 2.0.14)

 {{ app.request.getBaseURL() }} 

In later versions of Symfony (tested for 2.5), try:

 {{ app.request.getSchemeAndHttpHost() }} 
+127
Jul 19 '12 at 10:25
source share

You can use the new request method getSchemeAndHttpHost() :

 {{ app.request.getSchemeAndHttpHost() }} 
+48
Mar 25 '13 at 20:17
source share

The base url is defined inside Symfony\Component\Routing\RequestContext .

It can be removed from the controller as follows:

 $this->container->get('router')->getContext()->getBaseUrl() 
+32
May 2 '12 at 8:05
source share

Valid from Symfony v2.1 to v4. 1+

If you want to use the base URL for a Symfony application, you must use getSchemeAndHttpHost() combined with getBaseUrl() , similar to how getUri() works, except for the path to the router and the query string.

 {{ app.request.schemeAndHttpHost ~ app.request.baseUrl }} 

For example, if the URL of your Symfony website is located at https://www.stackoverflow.com/app1/ , then these two methods return the following values:

getSchemeAndHttpHost

 https://www.stackoverflow.com 

getBaseUrl

 /app1 

Note: getBaseUrl() includes the name of the script file (i.e. /app.php ) if it is /app.php in your URL.

+23
Dec 23 '15 at 17:40
source share

For Symfony 2.3+, to get the base url in the controller should be

 $this->get('request')->getSchemeAndHttpHost(); 
+15
Oct 04 '14 at 14:11
source share
  <base href="{{ app.request.getSchemeAndHttpHost() }}"/> 

or from the controller

 $this->container->get('router')->getContext()->getSchemeAndHttpHost() 
+13
Jul 23 '14 at 16:01
source share

Also for js / css / image URLs there is a useful asset () function

 <img src="{{ asset('image/logo.png') }}"/> 
+3
Apr 29 '16 at 15:10
source share

Instead of passing a global variable to a template, you can define a basic template and display the "global part" in it. A basic template can be inherited.

Example rendering template From symfony Documentation:

 <div id="sidebar"> {% render "AcmeArticleBundle:Article:recentArticles" with {'max': 3} %} </div> 
+2
Feb 01 '12 at 7:32
source share

For the current version of Symfony (at the time of this writing, this is Symfony 4.1), there was no direct access to the service container, as was done in some other answers.

Instead (if you are not using the standard service configuration), enter the query object using the type hint.

 <?php namespace App\Service; use Symfony\Component\HttpFoundation\RequestStack; /** * The YourService class provides a method for retrieving the base URL. * * @package App\Service */ class YourService { /** * @var string */ protected $baseUrl; /** * YourService constructor. * * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack */ public function __construct(RequestStack $requestStack) { $this->baseUrl = $requestStack->getCurrentRequest()->getSchemeAndHttpHost(); } /** * Returns the current base URL. * * @return string */ public function getBaseUrl(): string { return $this->baseUrl; } } 

See also the symfony white papers on how to get the current request object .

+1
Jun 11 '18 at 12:47
source share
 {{ dump(app.request.server.get('DOCUMENT_ROOT')) }} 
0
Nov 23 '14 at 19:32
source share

In one situation related to the app.request.getHttpHost application, app.request.getHttpHost helped me. It returns something like example.com or subdomain.example.com (or example.com:8000 when the port is not standard). This was in Symfony 3.4.

0
May 7, '19 at 18:06
source share



All Articles