php - Symfony2 Cannot instantiate interface in dependency injection -
i have problem when seek create service in symfony2.
this services.yml
services: menu: class: my\webbundle\classes\system\menu arguments: [@service_container] this service menu:
namespace my\webbundle\classes\system; utilize symfony\component\dependencyinjection\containerinterface container; class menu{ private $container; public function __construct(container $container) { $this->container = $container; } public function getmenu($section) { homecoming "hello"; } } when in controller do:
$menu = $this->get('menu'); i get:
fatalerrorexception: error: cannot instantiate interface my\webbundle\classes\system\menu in ...\cache\dev\appdevdebugprojectcontainer.php line 1522
of course of study cleaned cache.
change utilize statement to;
use symfony\component\dependencyinjection\container; also redeclare arguments enclosing service name in quotes.
services: menu: class: my\webbundle\classes\system\menu arguments: ["@service_container"] i question why injecting whole container though. why not inject parts need?
if defining controller service also, can access menu service injecting controller.
extra per comment request;
services: menu: class: my\webbundle\classes\system\menu arguments: ["@router"] and class like;
<?php namespace my\webbundle\classes\system; utilize symfony\component\routing\routerinterface; class menu{ private $router; public function __construct(routerinterface $router) { $this->router = $router; } public function getmenu($section) { homecoming "hello"; } } php symfony2 dependency-injection
No comments:
Post a Comment