override parameter of anohter bundle - symfony

I'm building a bundle, which depends on another bundle. The parent bundle loads a services.yml file, which defines some parameters:
parameters:
xbundle.doctrine.factory: Doctrine\ORM\Repository\DefaultRepositoryFactory
services:
....
I know the xbundle.doctrine.factory parameter can be changed from app/config/config.yml, but I want to change its value the from within my custom child bundle. I read the docs, and also the suggested stackoverflow questions, but still can't figure how to achieve it.

You must write a CompilerPass in your child Bundle, and change the value:
// src/Acme/DemoBundle/DependencyInjection/Compiler/OverrideServiceCompilerPass.php
namespace Acme\DemoBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class OverrideServiceCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container->setParameter('xbundle.doctrine.factory', '..New Value ...');
}
}
Some documentation here.

Related

Can't use Route

Route("/) does not work.
I already tried to reinstall annotations and Symfony with Composer.
My routes.yaml :
index:
path: /
controller: App\Controller\ArticleController::homepage
My ArticleController.php :
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ArticleController
{
/**
* #Route("/")
*/
public function homepage()
{
return new Response("TEST");
}
/**
* #Route("/news/test-test")
*/
public function show()
{
return new Response("FUTURE");
}
}
The page always shows the public/index.php, but it should show "FUTURE" ;)
in your config folder you should find a folder called routes inside it should be a file called annotations.yaml which contains the following :
controllers:
resource: ../../src/Controller/
type: annotation
if you have this you should get "TEST" when you go to ("/")
and you get "FUTURE" when you go to (/news/test-test)
and make sure to clear the cache if it did not
and you have to comment every thing inside routes.yml
Like Ali Mhanna said, check first annotations.yaml.
Then, according to Symfony documentation (read it, it can help ;), run php bin/console debug:router
If you see your route, your route is working.

Symfony 2.3 FOSUserBundle.en.yml not registering override

I am trying to override the default FOSUserBundle.en.yml from the FOS user-bundle.
I have the user bundle working fine, i have registered a user and logged in.
However when I copy the FOSUserBundle.en.yml into my own UserBundle to override the wording Symfony doesn't seem to pick it up.
This is the path I have copied the transation file to:
src/Blogger/UserBundle/Resources/translations/FOSUserBundle.en.ym
But no joy.. I have cleared the caches tried another browser but the change in the override will not come through.
I can place the change the in the original and see the changes:
vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/translations/FOSUserBundle.en.yml
Am I missing something?
Thanks,
John
To override the bundle, you should add this :
// src/Blogger/UserBundle/BloggerUserBundle.php
namespace Blogger\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class BloggerUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Here's a link from the docs : http://symfony.com/doc/current/cookbook/bundles/inheritance.html
All the steps you should follow are in there.

Overriding the FOSUserBundle controllers

So I read alot about the overriding of templates and such and overriding of bundles in Symfony.
I am using the new Symfony 2.3, I have not tried this in lower versions of Symfony.
I followed the tutorial about overriding bundles in Symfony:
http://symfony.com/doc/2.3/cookbook/bundles/inheritance.html
I followed the tutorial about overriding the controllers of FOSUserBundle, which is the same thing really:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_controllers.md
I had a bundle named Acme/WebBundle.
Now I have done the following things:
Created a new bundle named Acme/UserBundle.
Created the file AcmeUserBundle.php in this bundle.
<?php
namespace Acme\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Created the following file structure:
-src
-Acme
-UserBundle
-Controller
RegistrationController.php
-Entity
User.php
-Resources
-translations
-views
AcmeUserBundle.php
In RegistrationController.php I set the namespace to:
namespace Acme\UserBundle\Controller;
Copied the contents of the registration controller of FOSUserBundle to mine.
Added to the beginning of registerAction()
die("message");
Now when I go to the registration form, the default /register route, I don't get a die, everything works fine. It does not see my bundle as a child, nothing is overridden and I've been trying to get it to work for ages hence my question here.
Did I do something wrong?
Remember that you need to add any new bundle to AppKernel::registerBundles() in app/AppKernel.php like this:
$bundles = array(
...
new Acme\UserBundle()
);

stripslashes inside Twig template

i want to use the php stripslashes function inside a twig template but this function is not a standard twig function, so i have to add it to twig as an extension, i tried this code inside a controller, but it doesnt work:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class XController extends Controller
{
public function YAction($page)
{
$twig=$this->get('twig');
$twig->addFunction('functionName', new Twig_Function_Function('someFunction'));
...
do i need a use statement for the "Twig_Function_Function" class?
am i doing this wrong?
If you want to use it in your twig templates, you don't need to make any add or call inside your controller, Read the How to write a custom Twig Extension section of the documentation.
Basicaly, you need to create an Extension Class that extends \Twig_Extension , then you need to register it as a service using the twig.extension tag. And finally you need to implement the getFunctions() method in order to add customized twig functions.
But in your case better is to add a filter, with the same logic you can also add a getFilters() method in your extension class so that you can specify your customized filters.
Also, take a deeper look at the Extending Twig section of the documentation to understand all the ways twig can be extended.
Or {{ function('stripslashes', "This\\'ll do") }}
(or apply stripslashes() when building your Twig context)
But, also, if you do this in php:
add_filter('timber/twig', function(\Twig_Environment $twig) {
$twig->addFunction(new Twig\TwigFunction('stripslashes', 'stripslashes'));
return $twig;
});
Then this works in twig:
{{ stripslashes("This\'ll do...") }}

Overriding the User Admin Form

I'm trying to override the SonataUser/Admin/Model/UserAdmin's configureFormFields() because I need to remove some default fields from the admin form.
So I have copied the file vendor/bundles/Sonata/UserBundle/Admin/Model/UserAdmin.php in my bundle app/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php and modified it. Then declared it as a service:
# app/application/Sonata/UserBundle/Resources/config/services.yml
services:
application_user.registration.form.type:
class: Application\Sonata\UserBundle\Admin\Model\UserAdmin
arguments: [%sonata_user.model.user.class%]
tags:
- { name: form.type, alias: application_user_admin }
Now questions:
Am I doing right ? How can I tell sonata admin to use it ?
The overriding class should be set in config.yml:
# app/config/config.yml
sonata_user:
admin:
user:
class: MyCompany\UserBundle\Admin\Model\UserAdmin
Extend original UserAdmin:
namespace MyCompany\UserBundle\Admin\Model;
use Sonata\AdminBundle\Form\FormMapper;
class UserAdmin extends \Sonata\UserBundle\Admin\Model\UserAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
// new logic
}
}
Of course change class name MyCompany\UserBundle\Admin\Model\UserAdmin to reflect your bundle structure.
It is a better practice to keep your bundles in the src directory instead: (See Creating a bundle section).
In this case, if you are using easy extends, make sure to use --dest=src in order to generate the bundle inside an Application namespace in src/.
php app/console sonata:easy-extends:generate SonataUserBundle --dest=src
By creating your overriding bundle in src/Application/Sonata/UserBundle and registering the vendor bundle as a parent, you won't have to create a new service. This explains you how to override the bundle properly: overriding a bundle and should save you a lot of time.
Don't forget to create the file you want to override in the same location as your parent bundle.
In your case, you would have to copy paste SonataUser/Admin/Model/UserAdmin.php from the vendor into your bundle src/Application/Sonata/UserBundle/Admin/Model/UserAdmin.php and modify it as you wish.
That's why overriding bundles can be so useful.

Resources