Symfony2 base url


Getting base url in symfony2 web application

Method 1

Define it in router

class HomeController extends Controller
{
/**
* @Route(“/”, name=”homepage”)
* @Template()
*/
public function indexAction()
{
return array();
}
}

and use it in Twig template

{{ url(‘homepage’) }}

Method 2

Using container

$this->container->get(‘router’)->getContext()->getBaseUrl();

Method 3

Using twig global variable

{{ app.request.getBaseURL() }}

Copy from http://www.ardianys.com/2013/01/symfony2-base-url.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s