Viewed   60 times

In a Symfony2 app's routing configuration, I can refer to a file like this:

    prefix: someprefix
    resource: "@SomeBundle/Resources/config/config.yml"

Is there any way to access a file relative to the bundle within a controller or other PHP code? In particular, I'm trying to use a SymfonyComponentYamlParser object to parse a file, and I don't want to refer to that file absolutely. Essentially, I want to do this:

$parser = new Parser();
$config = $parser->parse( file_get_contents("@SomeBundle/Resources/config/config.yml") );

I've checked out the SymfonyComponentFinderFinder class, but I don't think that's what I'm looking for. Any ideas? Or maybe I'm completely overlooking a better way of doing this?



As a matter of fact, there is a service you could use for this, the kernel ($this->get('kernel')). It has a method called locateResource().

For example:

$kernel = $container->getService('kernel');
$path = $kernel->locateResource('@AdmeDemoBundle/path/to/file/Foo.txt');
Wednesday, September 21, 2022

You need to give the array_shift() the parameter! Look this example:

$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack); // Here you give the parameter

You give the null parameter on array_shift() and you need to change it!


array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. Read here for more

Wednesday, October 19, 2022

file_put_contents creates the file if it doesn't exist, but it fails if you try to put the file in a directory that doesn't exist. So you should try the following:

  • check if the images directory exists
  • check the write permissions of the directory
  • try with an absolute path, so in your case probably $target_dir = '/home/ragiththomas/Sites/asco-forum/images/';
Friday, December 9, 2022

I am also new to Symfony and will follow the results of this question with interest, but for what it's worth, my take on it is:

A bundle is just that: a group of files, assets, PHP classes and methods, tests, etc. The logic of the grouping can be anything you like. In some cases, it's really obvious what the grouping is and why it's been done -- for instance, if I wrote a blog system for Symfony2 and wanted to release it, I'd make it into a bundle. That's the sort of example used most in the documentation.

But you'd also use bundles for anything you wanted to release as one little feature. Say for instance, this bundle which creates default routes for all your controllers. It's not a fully developed plugin/feature like a blog or forum, but it's a bit of code that I can easily import into my project, it stays totally separate from everything else, it's a bundle.

Finally, you'd also use bundles internally to your project, in absolutely any way which makes sense to you.

My take on your specific situation:

Quick and easy:

  • MySiteMyCode -- gets the job done, and maybe you don't have any logical way to break up the code you're going to write.

If there's some more unique features between the two sites and you want to separate them out for clarity:

  • MySiteSharedFeatures
  • MySiteSite1Features
  • MySiteSite2Features

If you really like everything in its place, or if you have a complex project, maybe:

  • MySiteMySiteMain (shared features and catch-all miscellany that doesn't deserve its own bundle)
  • MySiteNews
  • MySiteSite1FeatureSomethingOrOther
  • MySiteSite2FeatureSomethingOrOther

I definitely think you want to stick to logical groups of code -- so I think your example "bundles Site1News and Site2News" and "MySiteSite1News and MySiteSite2News" wouldn't be the best way to go. Site1 and Site2 are implementations, so making a separate bundle for each site's news page would seem to be counterproductive to me; you'd want to make one news component and build it to be used in two different ways.

As for your two-domains question, you can either point both domains at the same code, and test within your code for what domain is being requested, or you can check out two copies of the same code and change the configuration files slightly (this doesn't necessarily violate the idea of DRY because you'd still edit the code in one place, then update both copies.)

Monday, October 31, 2022

It really depends on how you include the header files.

If you include with double-quotes, like e.g.

#include "some_header_file.h"

Then the relative path is from the current files location.

If you include using angle-brackets, like e.g.

#include <some_header_file.h>

Then the relative path is based on the system include paths.

You can always add a path to the system include path. How to do it depend on your environment and compiler. If you're using Visual Studio you go into the project properties dialog, and in the "C/C++" / "General" tab there is a field called "Additional Include Directories" where you can add directories. (This is for VS 2015, might be a little different on other versions.)

Regarding double quotes inclusion. Lets say your project hierarchy looks like this (on disk!):

|-- Include
|-- Source
|   `-- MoreSource
`-- Other

In Project/Source you have your source files, and if one of them want to include a header file from Project/Include, then it will look something like

#include "../Include/header.h"

Now if you have a source file in Project/Source/MoreSource that want to include the same header file it will be

#include "../../Include/header.h"

It could be useful to add the Project/Include directory to the system header search path. You can still use double-quotes to include the files, since if they are not found then the preprocessor will search the system paths as well, but you don't need the "full" relative path. If you add Project/Include to the system header path, you could write just

#include "header.h"


#include <header.h>

Be careful though, if you have a header file with the same name as an actual system header file you might have some trouble.

Sunday, October 16, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :