Comments (8)
$this->get()
is just a helper method that uses the service container and injecting it is a bad practice. One of the reasons for defining controllers as services is to avoid injecting the container. Instead, you inject only those services that you need.
Just add a parameter for the mailer
service to the constructor of your contoller and inject it.
from elnurabstractcontrollerbundle.
how can I inject the doctrine EM? I've tried for like 6 hours now!
from elnurabstractcontrollerbundle.
The service name is doctrine.orm.entity_manager
.
from elnurabstractcontrollerbundle.
Ugh! Where is this service defined? What is this format? 'doctrine.orm.entity_manager'? It's similar to \Doctrine\ORM\EntityManager but it's looking different. Why the underscore in 'entity_manager'?
On SF2 site I see:
"In YAML, the special @my_mailer syntax tells the container to look for a service named my_mailer and to pass that object into the constructor of NewsletterManager. In this case, however, the specified service my_mailer must exist. If it does not, an exception will be thrown. You can mark your dependencies as optional - this will be discussed in the next section."
This is why I want to know where 'doctrine.orm.entity_manager' service exists. It seems SF2 can't find?
I'm getting this exception:
_Catchable Fatal Error: Argument 1 passed to HostingChoice\Controller\WebController::_construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /home/path/app/cache/dev/appDevDebugProjectContainer.php on line 3036 and defined in /home/path/src/Acme/Controller/WebController.php line 28
use Doctrine\ORM\EntityManager;
/**
* @Service("WebController", parent="elnur.controller.abstract")
*/
class WebController extends AbstractController
{
protected $em;
public function __construct(\EntityManager $em) { [...] }
(Instead of \EntityManager I've also tried (\Doctrine\ORM\EntityManager $em) and (EntityManager $em) in the function signature.)
Here is my config.yml file:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
# auto_generate_proxy_classes: %kernel.debug%
# auto_mapping: true
mappings:
Acme:
type: annotation
dir: %kernel.root_dir%/../src/Acme/Model
prefix: Acme\Model
alias: Model
is_bundle: false
services:
WebController:
class: Acme\Controller\WebController
arguments: [ @doctrine.orm.entity_manager ]
parent: elnur.controller.abstract
from elnurabstractcontrollerbundle.
Use this command to find the entity manager service name:
app/console container:debug | grep -i entity
from elnurabstractcontrollerbundle.
:(
http://stackoverflow.com/questions/22678106/how-can-i-inject-doctrine-into-symfony2-service
from elnurabstractcontrollerbundle.
Nothing is working my friend. I love your approach but it's hard for me to inject services. I want to use your controller and abstract away from SF2 as much as possible but I can't get the damn thing to inject an EM! Ugh :((
from elnurabstractcontrollerbundle.
Answered your question on SO.
from elnurabstractcontrollerbundle.
Related Issues (8)
- How to configure routing when using controller as service? HOT 5
- Symfony2 logging HOT 4
- How to redirect with ElnurAbstractController HOT 1
- Unit testing when controllers are outside bundles HOT 1
- How to inject SF2 Container? HOT 1
- Embedding Controller HOT 1
- Passing parameters from parameters.yml to abstract controller HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elnurabstractcontrollerbundle.