In twig.yaml I have definded global path:
paths:
'%kernel.project_dir%/public/assets/media': images
And now I would like to get this path (as a full real path) in one of my symfony class.
How can I do it?
Thank you.
You could inject Twig\Loader\FilesystemLoader in your service and get the path through $loader->getPaths('images'). The container builder should already have the paths resolved for you - does that help?
Related
I am currently building my own Symfony bundle (I am using version 3.3). It works fine so far, but now I wanted to move the bundle-specific service definition out of my app/config/services.yml to a service definition within the bundle.
I created a src/MyBundle/Resources/config/services.yml and followed the guide How to Load Service Configuration inside a Bundle to load it. That works just fine, but I thought that the paths inside the newly created services.yml look a bit ugly:
MyBundle\:
resource: '../../*'
exclude: '../../{Tests}'
So I thought it would look a little cleaner, if I use the #Bundle-notation instead:
MyBundle\:
resource: '#MyBundle/*'
exclude: '#MyBundle/{Tests}'
However, than I was facing the error:
FileLocatorFileNotFoundException
The file "#MyBundle" does not exist (in: <...>\MyBundle\DependencyInjection/../Resources/config), where <...> corresponds to an absolute path.
I also tested whether it was a naming issue of the bundle name, but when I have a wrongly named bundle reference in the app/config/services.yml, e.g. #SomeBundleThatDoesNotExist then I get a different error:
FileLoaderLoadException
Bundle "SomeBundleThatDoesNotExist" does not exist or it is not enabled.
So my question is: Why does the #Bundle-notation work fine in the app/config/services.yml but not in the src/MyBundle/Resources/config/services.yml?
If you use the service remember make public for external use:
autowire: true
public: true
But, if your symfony dont know where is the bundle please chec this answer:
Symfony generated Bundle doesn't work
I have 2 bundle MyBundle1 and MyBundle2 , MyBundle2 inherits MyBundle1.
In /app/config/services.yml i have:
imports:
- resource: '#MyBundle1/Resources/config/services.yml'
- resource: '#MyBundle2/Resources/config/services.yml'
The problem is that '#MyBundle2/Resources/config/services.yml' will be considered as '#MyBundle1/Resources/config/services.yml' because it inherits it, so override its files.
My question is: How can I access to the real '#MyBundle2/Resources/config/services.yml', not the one rewritten ?
Thanks
The Symfony cookbook explains that Bundles override their parent Bundles resource files when
they're in the same location inside the bundle
They've been imported using the #MyBundle/...../services.yml (the #-part) syntax.
In order to have your Bundle not override your parent Bundles services you can use a different filename (eg. _services.yml) or refer to it using the full path in your config.
EDIT | I tried prefixing controllers class name with "Web" - no dice :(
According to everything I have read and previously experienced:
http://symfony.com/doc/current/best_practices/controllers.html
I should be able to move/organize controllers into sub-folders but now I cannot seem to get Symfony to cooperate:
AppBundle/Controller
- MemberController.php
- DefaultController.php
Works fine - but if I move
AppBundle/Controller
/web/MemberController
- DefaultController
Then clear the cache - I get the following error(s):
[Symfony\Component\Config\Exception\FileLoaderLoadException]
Class AppBundle\Controller\MemberController does not exist in /var/www/project/src/AppBundle/Controller (which is being imported from "/var/www/project/app/config/routing.yml").
[ReflectionException]
Class AppBundle\Controller\MemberController does not exist
Of course it doesn't I moved it up down one directory into a sub-folder "web". What gives? My routing.yml is as simple as:
site:
resource: "#AppBundle/Controller"
type: annotation
I'm trying to use Symfony2 without bundles. When my routing.yml looks like this:
app:
resource: "/var/www/oo_talks/src/UI"
type: annotation
Everything works in browser. But how do I get rid of full path here if I do not use bundles? How can I reference root project folder?
And still, running console command gives error about being unable to load a resource.
Added later, based on comments:
resource: "%kernel.root_dir%/src/UI" doesn't work as well as resource: "%kernel.root_dir%/../src/UI". Still "Cannot load resource" error.
Adding parameter with full path and referencing it doesn't work either.
By default relative path is resolved by file_locator service relatively to the %kernel.root_dir%/Resources directory or relatively to the directory of the file where path was specified.
I am trying to integarte IvoryCKEditor with sonata media bundle. Perpose is to allow image uploads in ckeditor. I tried it using a CoopTilleulsCKEditorSonataMediaBundle but i keep getting an error:
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "admin_sonata_media_media_ckeditor_browser" as such route does not exist.") in "IvoryCKEditorBundle:Form:ckeditor_widget.html.twig".
Thanks :)
You should check your routes with the command:
$ php app/console debug:router | grep ckeditor
Then you have to replace admin_sonata_media_media_ckeditor_browser and admin_sonata_media_media_ckeditor_upload by routes founded with this command, in the config of ivory_ck_editor (maybe in your config.yml or your ivory_ckeditor.yml file)
If you don't have route for ckeditor, I think you should check if you install correctly the bundle.
1) install SonataFormatterBundle 2) add the bundle and its dependencies in AppKernel 3) config files 4) CLEAR CACHE 5) roll
Reason of the issue
This problem arises when trying to integrate CKEditor through the
SonataFormatterBundle without using the SonataAdminBundle.
In fact, the integration proposed by the SonataFormatterBundle is meant to only work for the SonataAdminBundle, and no easy integration for a custom admin bundle is currently available.
Note that this dependency is not specified in the documentation at the moment.
How to solve the problem
Simply install the SonataAdminBundle following this installation process. You do need to configure the bundle entirely as specified in the documentation. To add the routes that were missing, such as admin_sonata_media_media_ckeditor_browser, simply add the following to your config/routes.yml:
# This is your custom admin bundle
admin:
resource: "#AdminBundle/Controller/"
type: annotation
prefix: /admin/
# Import SonataAdminBundle routes
admin_area:
resource: "#SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
prefix: /admin/sonata # put whatever prefix here
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
Just to add something.
If you are SURE that you have installed, all dependencies, and set all configs correctly, and still getting this error, then the cause may be that you have defined your 'sonata_media' prefix in routing.yml differently.
You can check all urls in your project in:
app/cache/dev/appDevUrlGenerator.php
In there you will find ALL routes in a variable $declaredRoutes
Afterwards simply put correct route name for browsing.
It will not work without SonataMediaBundle - you will need to install it with SonataFormatterBundle
Sonata become too complicated as for me, so I just use IvoryCKEditorBundle directly