Symfony – How to get parameters value in Controller and Twig

In Symfony, we defined various parameters in Parameters.yml so that our application can use these in various places. Example –

    database_port: null
    database_name: symfony
    database_user: root
    database_password: ''
    mailer_transport: smtp
    mailer_encryption: tls
    mailer_user: john
    mailer_password: ''
    secret: your-secret-key

But how can we access these values in our Controller or Twig ? Simple 🙂 using getParameter function you can easily access these.

$dataBaseName = $this->container->getParameter('database_name');

To access these parameters you have to define these in Config.yml file, under twig key with globals keyword like database_name

# Twig Configuration
        database_name: %database_name%

using %% you can access defined values like we use $ in php for variables.

Twig template code

{{ database_name }}

Symfony version used – 2.7

. . .

Comments (2)

Add Your Comment

  • Potworny
    Thank you 🙂
    • Webkul Support
      You are most welcome.
  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again