Sylius: Overwrite BackendMenuBuilder - php

I start developing a project based on Sylius and after install process I want to customize Backend Dashboard. As Sylius works with services, I found that BackendMenuBuilder is a service called "sylius.menu_builder.backend". With this in mind and some googling I edited my "services.yml" in AppBundle/Resources/config" and put this lines:
services:
sylius.menu_builder.backend:
class: AppBundle\Menu\Backend\BackendMenu
I created a new class "BackendMenu" in "AppBundle\Menu\Backend" to be called instead the "default" Sylius service. I've got some error in my first tries but after I could overwrite the default service in this way. Is this the right way to do that? For any new service that I want to not use the default version should I insert this new parameter in my "services.yml"? I wonder this because I think developing a medium/big project over Sylius platform and maybe it could be a handy problem.

Instead overwriting whole service definition You can just provide Your own class paramerter. Example:
parameters:
sylius.menu_builder.backend.class: AppBundle\Menu\Backend\BackendMenu
Then You don't need to redeclare all arguments (if there are any).
Other ways it to do this with ComplierPass - http://symfony.com/doc/current/bundles/override.html#services-configuration

Related

Override parameters.yml in each bundle symfony2

I'm working on a complex Symfony project. In the project, we have a core bundle which uses the parameters.yml located in app/config.
Each other AppBundle will inherit this CoreBundle and will correspond to a website.
What I need is to have specific parameters in each bundle that will override the default parameters one: when I'll use a route that will bring me into a controller's bundle, the parameters of this specific bundle have to override all the other ones.
I've tried the preprend method but it doesn't fit to this need. It only allows me to create new parameters for this bundle, but not to override the other ones.
I think you misunderstand the idea of bundles in Symfony. Bundle by design should be a reusable module, therefore the configuration placed inside a bundle is the default one. Then it is possible to override parameters (and not only!) in configuration in app folder.
The basic idea is:
Bundles don't use application. Application uses bundles.
So it's completely the opposite to what you expect it to be. Acutally it makes no sense to condition whole application configuration depending on current route since bundles can use one another. What if your currenct action will internally (without redirect) call another bundle's service or even controller?
Also it's worth mentioning that the app folder is the final folder for your application, therefore you can override in it not only bundle's configuration but also other things like services, view templates and so on.
Edit: I forgot to suggest a solution for you :)
Since you want to use custom parameters inside bundle, why do you need the default value in first place? Just create separate parameter namespace for each bundle that won't be overridden by application config. Then use it only inside that bundle.
Solution found thanks to dragoste's asking about separated kernels.
To solve my problem, I had to split the kernels : one for each website.
Documentation can be found here :
http://jolicode.com/blog/multiple-applications-with-symfony2

Where to Place My Package in Lumen Framework

If I want to use some custom class in Lumen, where should I place them? The Laravel official document does not mention this in any of application structure, service container or package development. Actually I found the document quite confusing to some extents.
For example, I want to set up a service called Invitation, I know I need to register this class in InvitationServiceProvider but where should I place the Invitation.php which the actual class exists in? This package is used for some specific application thus I do not want to put it in composer packagist.
BTW, the version of Lumen Framework is 5.2.
So finally I created a folder named Service under app and just pull all the libraries inside...

Common lib on symfony2

Before using Symfony2, I used to have a common lib with a lot of simple but useful functions (for instance, a function which takes "Azè_rtï" in argument and returns "aze-rti").
So, well, I created a Bundle: CommonLibsBundle.
But.. I have only one or two php files. It does not make sense to me to use a controller / view / model in this kind of situation.
What should I do? May I erase all folders in my new bundle (Controller, DependencyInjection, Resources, Tests... + CommonLibsBundle.php) and just put my lib.php in it?
Many thanks,
Bliss
Unless you need to tap into the Symfony framework itself - for configuration or to define services, it doesn't need to be a bundle - it's just a library. Give it a reasonable namespace, and call as required as you would any other component or library.
Even if you wanted to add Symfony-specific services that you could call, there is something to be said to still have an external simple library - usable anywhere, which then is wrapped by a very thin bundle which would only add the Symfony-specific (or Laravel, or ZF, or whatever) services and configuration as required.

Extending of Symfony container

is it possible overwrite/extend Symfony\Component\DependencyInjection\Container::get() method? I want automatic creating service, when it is not contain in container, but class of service exists.
For example:
Name of service is My.MyBundle.Model.FooRepository
Service with this name doesnt exists, but when i call:
$container->get('My.MyBundle.Model.FooRepository');
check class_exists for \My\MyBundle\Model\FooRepository and when its exists, add to container and return it. Dependencies of this new services will be resolve by kutny/autowiring-bundle.
This feature can be extended only for some namespaces or interfaces and in production enviroment can be cached, but for developing will be great helper.
Any idea?
This is not directly answering your question but maybe it's answering your need: if you want to have "auto-wiring" inside your Symfony project, you can use PHP-DI inside Symfony. PHP-DI is an alternative container that can do auto-wiring (which Symfony does not).
Have a look at the Symfony 2 integration documentation to see if it can fit your bill.

Laravel: how to use my composer installed bundle?

I am learning Laravel. With composer i included the mobile-detect-bundle in the installation (files are in the folders). When i use the code as stated in the docs on github
$mobileDetector = $this->get('mobile_detect.mobile_detector');
i get this error:
**ErrorException**
File does not exist at path mobile_detect.mobile_detector (View: ) (View: )
I use this in my blade view and i think i have to set the path of 'mobile_detect.mobile_detector' but i have no clue what it has to be. Maybe one can give me a push in the right direction?
The reason it's not working is because you are trying to use a Symfony 2 bundle inside Laravel right out of the box.
As the github page says:
Symfony2 bundle for detect mobile devices, manage mobile view and redirect to the mobile and tablet version.
Basically, the line that you are trying to run is the way that you use services inside Symfony. It will work if you are in a Symfony application, but not inside Laravel.
// Get the mobile_detect.mobile_detector
// service from Symfony's service container
$mobileDetector = $this->get('mobile_detect.mobile_detector');
Although there might be some way to make it work, I'd sugggest to search in packagist for a Laravel specific package, or a PHP generic one that provides the same functionality, to make your life easier.
I've made a search and found this one, which is based in Mobile Detect too:
https://github.com/jenssegers/Laravel-Agent

Categories