Using Twig Variables inside config.yml - symfony

Is it possible to use variable values for defining new twig globals in a configuartion-yml-file?
The situation is, that we want to define the domain name at the one hand and at the other define often used paths (but using the already defined variable instead of writing the server name again at the beginning of each path).
So is it possible to make the config-file look something like the following code?
twig:
globals:
serv: http://www.server.de
path: {{serv}}/path/
If not, what would be a reasonable alternative?
Thx in advance

I suggest to use parameters.yml to define configuration params.
parameters.yml
serv: "http://www.server.de"
config.yml
twig:
globals:
serv: "%serv%"
path: "%serv%/path/"

Related

create parameters in parameters.ini in Symfony2 and access in all Twig Files

How to create Constant in Symfony2 from a variable defined in parameters.ini so I can use that constant in all twig Files.
Example:
parameters.ini:
blog="https://www.blog.com"
Constant value declaration where I have to declare so that it accessible in all twig files.?
'blog_link' => $this->container->getParameter( 'blog' )
view:
BLOG
You can simply add Twig global variable like this:
#app/config/config.yml
twig:
globals:
blog_link: '%blog%'
Where %blog% is a parameter defined earlier in parameters.ini just like you've already did.
More info in Symfony's Cookbook

How can I access parameters defined in parameters.yml from within a twig template?

I'm using Symfony2.
How can I access parameters defined in parameters.yml from within a twig template ?
Thanks.
Define it in config.yml like this:
twig:
globals:
some_key: %some_key%
and then use in twig files like:
{{ some_key }}
For security reasons you can't get access to data from parameters.yml in the templates without defining global twig variables.

Can I organize Doctrine entities or their mapping files into subfolders?

I have tried this directory structure for my mapping files:
/config/doctrine/Place.orm.yml
/config/doctrine/Place/Type.orm.yml
/config/doctrine/Place/Price.orm.yml
And have pointed to the corresponding Entity in my mapping file like this:
Project\TestBundle\Entity\Place\Type:
type: entity
table: place_type
id:
id:
type: integer
generator: { strategy:AUTO }
fields:
name:
type: string
length: 255
But this returns an error. The system can't seem to detect the mapping file for Entity.
Long story short, this is possible.
If you have folder structure inside your bundle's Entity folder, it's simple. You have to name your ORM files using entity namespace part below of Entity namespace and replacing \ with ..
So if, for example, you have Project\TestBundle\Entity\Place\Type entity, the ORM file (located in config/doctrine folder inside bundle) will have name Place.Type.orm.yml.
If you want to use as Doctrine entities classes from outside of Entity folder (or even outside of the bundle folder), it gets a little bit complicated, but still possible. Doctrine Bundle allows to define custom mapping locations for your classes in its configuration.
Again - example. If you have your entities inside of Project\Test namespace (in folder src/Project/Test), you can define mapping like this:
app/config/config*.yml
doctrine:
orm:
MyCustomDomain:
mapping: true
type: yml
dir: %kernel.root_dir%/config/projecttest
alias: ProjectTest
prefix: Project\Test
is_bundle: false
In fact, Doctrine Bundle does something similar automatically, that's why you can put all of your classes in Entity subfolder and fear no more.
The prefix is namespace prefix. Folder is the path to folder with configuration files. Alias is interesting - it allows to use simpler object names in DQL queries and mapping files. Symfony's TestBundle:Test syntax works on the same premise - TestBundleis the alias for all entities in TestBundle. is_bundle tells Doctrine, that the entities are outside of Symfony bundle and require a little bit different treatment.
There are some caveats in defining your own mapping. Mapper works using 'first match' rule on prefix. So if you declare your mapping on too broad namespace prefix, it can override other mappings.
Nevertheless, it is useful sometimes. For example, if you want to map classes from "foreign" library directly to Doctrine. Or are creating a library not completely tied to Symfony and want to keep some of your classes outside of the bundle.
If you want to organize your doctrine Entities into subfolders
for example: src/AppBundle/Entity/subfolder/MyEntity.php
then the corresponding ORM file should look like:
src/Resources/config/Doctrine/subfolder.MyEntity.orm.yml
Don't make subfolders inside src/Resources/config/Doctrine/*
*Note: If you have a look at the Doctrine config docs it looks like it may be possible to configure a different location for your orm.yml file using:
doctrine:
orm:
# An array of mappings, which may be a bundle name or something else
mapping_name:
mapping: true
type: ~
dir: ~
alias: ~
prefix: ~
is_bundle: ~
but I have not tried this. maybe someone else can confirm to improve this answer?
with #ORM annotations all you need is ::class,
here is example:
/**
* #ORM\ManyToOne(targetEntity=User::class)
*/
private User $accountant;
don't forget to import User like this
use App\Entity\Subfolder\User;

Is a global variable for a Twig template available inside a Symfony2 controller?

A global variable for a Twig template can be defined inside the config.yml of a Symfony2 application as in the following:
twig:
globals:
var_name: var_value
Hence, in each Twig template the variable can be used as follows:
{{var_name}}
that will display
var_value
Do you know such a way to get the value of a global variable just inside a Symfony2 controller?
There doesn't seem to be a way to grab a particular value. But you can get the full array of globals from the twig service and then grab it's offset.
$twig = $this->container->get('twig');
$globals = $twig->getGlobals();
echo $globals['var_name'];
The right practice is to follow the official tutorial about Twig global variables in the Symfony's Cookbook. In practice, one should define a global variable that can be used in the controllers, e.g.:
; app/config/parameters.ini
[parameters]
my_global_var: HiAll
Then the variable is defined as global for the Twig templates
# app/config/config.yml
twig:
globals:
my_var: %my_global_var%
Hence, {{my_var}} will return HiAll but one should take care of the value once in the parameters.ini file.
So, the answer to the question is no! Or precisely, not in an effective way. MDrollette drawn a possible way!
I wasn't clear if you wanted to access the twig var in the controller, or just access a global var from the config. Here is how to access a global var from the config file...
You can place the value in the parameters section of the config..
parameters:
var_name: some_value
Now you can access it from the controller...
$value = $this->container->getParameter('var_name');
I think you'd better to use bundle configuration or DIC parameters for your value, and then add it to the twig globals (via your bundle extension class, for example), and not trying to do the opposite.

Reuse Bundle Symfony2

This seems like it should be easy, but I am unable to find the answer. How can one reuse a bundle multiple times within the same symfony project? For example if I have an article bundle that I want to use multiple times on the same website.
I see in the app routing.yml you can add a prefix to the routed URL's, however if I try this multiple times with a different prefix each time only the last one works. Assumedly because the unique route names within the bundle are not prefixed, just the routs.
Foo:
resource: "#Foo/Resources/config/routing.yml"
prefix: /bar/
Bah:
resource: "#Foo/Resources/config/routing.yml"
prefix: /bah/
So where do I go from here? Is there some way to auto prefix unique route names, database tables etc (while still being able to reference / link to everything from within templates). Or is this a situation that symfony has just not been designed to accommodate?
I believe that there are two options:
Create the object in the your bundle:
Inside of your foo controller, do something like this:
use Acme\BahBundle\Class;
You should then be able to call it
$class = new Class();
$class->function('params');
The other option is to register the bundle as a service, check out the doc for more info:
http://symfony.com/doc/2.0/book/service_container.html

Resources