2

I need to define such configuration parameters as the contacts of the company (phone, address etc) for using in twig templates. How am I supposed to make it? The most obvious way to me is adding them into the app/config/config.yml file like this:

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: en
    contacts:
        phone: 08455555050

Is that ok or there is any better way?

Also, the docs say I should use constants in entities for params that never change, but I don't need an entity for which would hold these data, how to be with this? I'm just starting with Symfony and confused.

2

2 Answers 2

2

Well I've made it this way:

app/config/services.yml:

parameters:
    locale: en
    contacts:
        phone: '123455678'
...
twig:
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    globals:
        contacts: '%contacts%'

Twig:

<span>{{ contacts.phone }}</span>
Sign up to request clarification or add additional context in comments.

2 Comments

A color hexadecimal is a constant. A phone number isn't, in any case. You need to put this parameter in the parameters.yml (don't forget about the parameters.yml.dist).
@Sergey Same as this: inanzzz.com/index.php/post/m8rt/… If you're learning symfony, I suggest you go check that blog. Symfony's documentation can be very superficial for the beginners.
0

Make a Service that return your Company

A Simple getCompany that return the doctrine entity

Register the service

# app/config/services.yml
services:

    AppBundle\Service\MyTwigExtension:
        class: YourService

Register the service in Twig global

# app/config/config.yml
twig:
    # ...
    globals:
        # the value is the service's id
        user_management: '@AppBundle\Service\MyTwigExtension'

And on your base twig template you can call the service like this

{% set Company = company.getCompanyInfo() %}

And anywhere you need Company details you can do Company.getMyField

I suggest you to make a entity so the user could get a backend to modify his company details, this will be more flexible is the future

1 Comment

Good idea, but far too complicated for the stage I am now at, I don't need any entities at all at this point. Seems like the only way I what I described in the original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.