I found 2 methods to get the root dir:
1. $rootDir = $this->get('kernel')->getRootDir() . '/../';
2. $rootDir = $this->container->getParameter('kernel.root_dir') . '/../';
I will use it from defaultAction().
Which one is preferred and why?
The difference is minor, you can use the two solutions.
The first use the Kernel service and the last use the ParameterBag.
Both are coming from use of container and returns a string representing the configured root_dir, by default the app/ directory.
Related
I need to display a yaml file as an array but i but I can't display it.
I have create a service, and my controller call this service.
In my service i try to call my yaml like this :
$value = Yaml::parseFile('public\assets\organizations.yaml');
return $value;
But that return me that error :
File "public\assets\organizations.yaml" does not exist.
You are specifying the file with a relative path. This will not be resolved relative to the file it is in but relative to the current working directory you are in when executing the script. Since this depends on various factors it will always be troublesome.
Thus you should always use absolute paths. In symfony you can get the base path of your project via the kernel.project_dir configuration parameter.
Your code does not provide enough context to understand how or where you are using it. But if it is inside a controller extending AbstractController you can use getParameter():
$projectDir = $this->getParameter('kernel.project_dir');
$absolutePath = $projectDir . '/public/assets/organizations.yml';
$value = Yaml::parseFile($absolutePath);
return $value;
Also note that using \ as the directory separator won't work under Linux/Unix-like systems where / is used as directory separator!
Since / will work as directory separator under Windows, too, it is easiest to use it for cross-OS compatibility. Alternatively use the DIRECTORY_SEPARATOR constant.
I'm using JMSI18nRoutingBundle for locale routing on our new site, but our existing site uses language + country in the following format and I need to keep the URLs looking the same.
example.com/us/en/hello (en_US)
example.com/be/fr/bonjour (fr_BE)
Is there any way to do this using config? If not, where is the best place to start customizing?
It doesn't look it's possible to do through config, but it can be done by replacing default implementation of PatternGenerationStrategyInterface by your own implementation.
You can check out default implementation that bundle uses here.
After you create your own implementation, just make bundle use your own implementation by setting the config parameter. If you're using YAML for example:
parameters:
jms_i18n_routing.pattern_generation_strategy.class: YourBundle\YourImplementationClass
Hint: you can basically copy/paste from default implementation and change line 69 to use str_replace('_', '/', $locale) instead of just $locale. That way, newly generated route pattern will contain a / if locale contains an _.
Not very elegant solution, but bundle unfortunately doesn't provide enough configuration to make it prettier.
How can I set that URL`s which have new in name are available only for ROLE_ADMINISTRATOR?
I`m sure that I must set it in security.yml access_control
But how to set for example
www.mydomain.com/user/new
www.mydomain.com/user/2/edit
only for ROLE_ADMINISTRATOR
{ path: ^/.*/new, role: ROLE_ADMINISTRATOR } doesn`t work :(
Symfony 2 will select the first pattern that matches. So, if a pattern above the one you shared gives access to that path (say something like ^/user) then the role of that line will be allowed.
Ensure that you have the restrictive paths above all of the others.
Also, I'm not sure if Symfony allows . to match /, so you might want to try ^/[\s\S]*/new (or even just /new$ if the new is always at the end) if it isn't just a problem with ordering.
I am trying to use ACL for my project, I have not done it before. I know only the concepts, what it is and why to use it.
I run this command :
$ php app/console init:acl
and I got five tables in my database .
My question is how to use these tables, means how data will be inserted in these tables .
I also have followed steps from here
and still not getting the hang of it , please help me out .
You should'nt use table directly (but you already know), but ACL Entities instead (but it's tricky).
Some people worked on bunbles to simplify those actions. Here is an example on how to use it :
https://github.com/Problematic/ProblematicAclManagerBundle
$comment = new Comment(); // create some entity
$aclManager = $this->get('problematic.acl_manager');
// Adds a permission no matter what other permissions existed before
$aclManager->addObjectPermission($comment, MaskBuilder::MASK_OWNER, $userEntity);
// Replaces all current permissions with this new one
$aclManager->setObjectPermission($comment, MaskBuilder::MASK_OWNER, $userEntity);
$aclManager->revokePermission($comment, MaskBUILDER::MASK_DELETE, $userEntity);
$aclManager->revokeAllObjectPermissions($comment, $userEntity);
You can apply permission on objects or directly on classes (upper level)
Is there a way to get the location of the Jamroot file, for use as a constant in another Jamfile in the project?
Right now, I have this kludge in my Jamroot:
constant HOME : [ os.environ HOME ] ;
constant MYPROJECT_ROOT : $(HOME)/src/myproject ;
And then later I might do something like this in another Jamfile, to allow me to include headers with paths from the root of the project.
<include>$(MYPROJECT_ROOT)
It's especially unsatisfactory because it means that if I share this project with others, they have to either keep it in exactly the same location relative to their $HOME or they have to update the Jamroot.
I'm interested in the smart way to do this specific include (instead of my ignorant beginner way of using constants). But I'd also be interested in solving the problem the way I asked -- by getting the Jamroot location into a constant -- because this might be useful in other ways too.
Use the path-constant rule.
path-constant MYPROJECT_ROOT : . ;
Then in sub-projects, you can get the directory of the Jamroot with $(MYPROJECT_ROOT).
Note that usually people name this variable TOP instead of MYPROJECT_ROOT, but that's just a convention.