I'm trying to get some params from a config yaml file, I'm doing the following:
namespace ExampleVendor\AdminBundle\Controller;
/**
* @Route("/", name="_home")
*/
public function index(ParameterBagInterface $params) {
$menuIcons = $this->getParameter('admin-aside-menu');
dump($menuIcons);
return $this->render('@ExampleVendorAdminBundle/index.html.twig');
}
As you can see is just a dump function that prints the admin-aside-menu
parameter, the problem is, this code works when it is in a controller in App\src\Controller, I mean in the "main" application src folder, but when I copy this code and paste it in a controller inside a custom bundle (something like vendor/myVendor/myBundle/src/Controller) I get an error when reloading the page:
Could not resolve argument $params of "ExampleVendor\AdminBundle\Controller\AdminController::index()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?
I need ParameterBagInterface
to get those parameters but I dont know how to "inject" it in my controller.
Okay so it seems like this post says how to fix this issue but, since the controller I need to inject something is in a custom bundle in the vendor folder, the answer doesn't help me at all
Project
|
+--AdminBundle <- My custom bundle, installed via composer as symlink
| |
| +--Controller
| | |
| | +--AdminController.php <- Heres where I want to get my parameter
| |
| +--DependencyInjection
| +--Entity
| +--Resources
| |
| +--config
| |
| +--custom.yaml <- There are my custom params
| +--services.yaml <- Heres where I should register my controller as service?
|
+--assets
|
+--bin
|
+--config
|
+--public
|
+--src
|
+--templates
|
+--vendor <- Heres my admin bundle as symlink
The AdminBundle
folder is installer via composer as symlink so I can use it in my project, so knowing this, anyone know how can I inject ParametersBag
or the parameter directly into my controller?
This is my custom.yaml that holds the params from my bundle
// AdminBundle/Resources/config/custom.yaml
parameters:
admin-aside-menu:
items:
- icon: 'Home/Chair2'
title: 'Prueba'
- icon: 'Home/Deer'
title: 'Prueba Venado'
This is the configuration class in the dependency injection
// AdminBundle/DependencyInjection/Configuration.php
class Configuration implements ConfigurationInterface {
public function getConfigTreeBuilder() {
$treeBuilder = new TreeBuilder('admin-aside-menu');
$treeBuilder->getRootNode()
->children()
->arrayNode('items')
->children()
->scalarNode('icon')->end()
->scalarNode('title')->end()
->end()
->end()
;
return $treeBuilder;
}
}
and this is the bundle extension
// AdminBundle/DependencyInjection/ExampleVendorAdminExtension .php
class ExampleVendorAdminExtension extends Extension {
/**
* Loads a specific configuration.
*
* @throws \InvalidArgumentException When provided tag is not defined in this extension
*/
public function load(array $configs, ContainerBuilder $container) {
$loader = new YamlFileLoader( $container, new FileLocator(__DIR__.'/../Resources/config') );
$loader->load('custom.yaml');
$loader->load('services.yaml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
}
}
I wrote this in my services.yaml to inject my parameter into my controller according to Florian's response
// AdminBundle/Resources/config/services.yaml
services:
admin-bundle.controller.admin-controller:
class: ExampleVendor\AdminBundle\Controller\AdminController
arguments:
- "%admin-aside-menu%"
it seems to work in a way because when I write "%admin-aside-menu%"
bad (a typo) an error page says
"Did you mean this: "admin-aside-menu"?"
So I think it is actually loading my param but in my controller I cannot "inject" it
This is the controller:
/**
* @Route(name="admin")
*
* Class AdminController
* @package ExampleVendor\AdminBundle\Controller
*/
class AdminController extends AbstractController {
public function __construct(array $adminAsideMenu) {
dump($adminAsideMenu);
}
}
but when I run this I get this error
The controller for URI "/admin/" is not callable. Controller "ExampleVendor\AdminBundle\Controller\AdminController" has required constructor arguments and does not exist in the container. Did you forget to define the controller as a service?