Coder Social home page Coder Social logo

Do I need to manually create new Swift_Mailer instance with this controller? Accessing service container about elnurabstractcontrollerbundle HOT 8 CLOSED

elnur avatar elnur commented on June 15, 2024
Do I need to manually create new Swift_Mailer instance with this controller? Accessing service container

from elnurabstractcontrollerbundle.

Comments (8)

elnur avatar elnur commented on June 15, 2024

$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.

samlevin avatar samlevin commented on June 15, 2024

how can I inject the doctrine EM? I've tried for like 6 hours now!

from elnurabstractcontrollerbundle.

elnur avatar elnur commented on June 15, 2024

The service name is doctrine.orm.entity_manager.

from elnurabstractcontrollerbundle.

samlevin avatar samlevin commented on June 15, 2024

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.

elnur avatar elnur commented on June 15, 2024

Use this command to find the entity manager service name:

app/console container:debug | grep -i entity

from elnurabstractcontrollerbundle.

samlevin avatar samlevin commented on June 15, 2024

:(

http://stackoverflow.com/questions/22678106/how-can-i-inject-doctrine-into-symfony2-service

from elnurabstractcontrollerbundle.

samlevin avatar samlevin commented on June 15, 2024

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.

elnur avatar elnur commented on June 15, 2024

Answered your question on SO.

from elnurabstractcontrollerbundle.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.