How to get Tempating or Container in custom Service?

Asked
Active3 hr before
Viewed126 times

8 Answers

containercustom
90%

Meta Stack Overflow , Stack Overflow Public questions & answers , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Thanks for contributing an answer to Stack Overflow!

It's not good idea to inject whole container. Better is to inject single dependencies:

use Symfony\ Bundle\ FrameworkBundle\ Templating\ EngineInterface;

class MyService {
   private $templating;
   public
   function __construct(EngineInterface $templating) {
      $this - > templating = $templating;
   }

   public
   function getView() {
      $this - > templating - > render('default/view.html.twig');
   }
}
88%

I have custom Service and I would like to use it in Twig templates. In Symfony < 3 I can do:,How to get Tempating or Container in custom Service? ,Cannot autowire service "AppBundle\Service\ViewService": argument "$container" of method "__construct()" references class "Symfony\Component\DependencyInjection\Container" but no such service exists. Try changing the type-hint to one of its parents: interface "Psr\Container\ContainerInterface", or interface "Symfony\Component\DependencyInjection\ContainerInterface".,It's not good idea to inject whole container. Better is to inject single dependencies:

I have custom Service and I would like to use it in Twig templates. In Symfony < 3 I can do:

use Symfony\ Component\ DependencyInjection\ Container;
//...
public
function __construct(Container $container) {
   $this - > container = $container;
}

public
function getView() {
   $this - > container - > get('templating') - > render('default/view.html.twig');
}
load more v
72%

I have custom Service and I would like to use it in Twig templates. In Symfony < 3 I can do:,Cannot autowire service "AppBundle\Service\ViewService": argument "$container" of method "__construct()" references class "Symfony\Component\DependencyInjection\Container" but no such service exists. Try changing the type-hint to one of its parents: interface "Psr\Container\ContainerInterface", or interface "Symfony\Component\DependencyInjection\ContainerInterface".,If you add any files,it will delete all existing files related to this answer-(only this answer),It's not good idea to inject whole container. Better is to inject single dependencies:

I have custom Service and I would like to use it in Twig templates. In Symfony < 3 I can do:

use Symfony\ Component\ DependencyInjection\ Container;
//...
public
function __construct(Container $container) {
   $this - > container = $container;
}

public
function getView() {
   $this - > container - > get('templating') - > render('default/view.html.twig');
}
load more v
65%

In Symfony, these useful objects are called services and each service lives inside a very special object called the service container. The container allows you to centralize the way objects are constructed. It makes your life easier, promotes a strong architecture and is super fast!,Your application is full of useful objects: a "Mailer" object might help you send emails while another object might help you save things to the database. Almost everything that your app "does" is actually done by one of these objects. And each time you install a new bundle, you get access to even more!,When you use these type-hints in your controller methods or inside your own services, Symfony will automatically pass you the service object matching that type.,If you need to fetch services lazily, instead of using public services you should consider using a service locator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// src/Controller/ProductController.php
namespace App\ Controller;

use Psr\ Log\ LoggerInterface;
use Symfony\ Component\ HttpFoundation\ Response;
use Symfony\ Component\ Routing\ Annotation\ Route;

class ProductController {
   /**
    * @Route("/products")
    */
   public
   function list(LoggerInterface $logger): Response {
      $logger - > info('Look, I just used a service!');

      // ...
   }
}
load more v
75%

A class can create an instance of the MyDependency class to make use of its WriteMessage method. In the following example, the MyDependency class is a dependency of the IndexModel class:,Doesn't create an instance of MyDependency, it's created by the DI container.,Registration of the dependency in a service container. ASP.NET Core provides a built-in service container, IServiceProvider. Services are typically registered in the app's Program.cs file.,The use of an interface or base class to abstract the dependency implementation.

Correct:

public class MyClass
{
    private readonly IOptionsMonitor<MyOptions> _optionsMonitor;

    public MyClass(IOptionsMonitor<MyOptions> optionsMonitor)
    {
        _optionsMonitor = optionsMonitor;
    }

    public void MyMethod()
    {
        var option = _optionsMonitor.CurrentValue.Option;

        ...
    }
}
load more v
40%

An injector creates dependencies, and maintains a container of dependency instances that it reuses if possible., An injector creates dependencies, and maintains a container of dependency instances that it reuses if possible. ,A provider is an object that tells an injector how to obtain or create a dependency., A provider is an object that tells an injector how to obtain or create a dependency.

      content_copy

      export class Logger {
         log(msg: any) {
            console.log(msg);
         }
         error(msg: any) {
            console.error(msg);
         }
         warn(msg: any) {
            console.warn(msg);
         }
      }
load more v
22%

A template describes a set of objects that can be parameterized and processed to produce a list of objects for creation by OpenShift Container Platform. A template can be processed to create anything you have permission to create within a project, for example services, build configurations, and deployment configurations. A template may also define a set of labels to apply to every object defined in the template.,You can use the CLI to process templates and use the configuration that is generated to create objects.,By doing this, the build configuration created by the template will now point to your fork of the application code, and you can modify the code and rebuild the application at will.,Object reports new ReplicationController and deployment available (this honors readiness probes defined on the object)

$ oc create -f <filename>
load more v
60%

bind: bind-mounts a directory or file from the host into the container.,A npipe mounts a named pipe from the host into the container.,The following example bind-mounts a host directory at /path/in/container in the containers backing the service:,volume: mounts a managed volume into the container.

$ docker service create[OPTIONS] IMAGE[COMMAND][ARG...]
load more v

Other "container-custom" queries related to "How to get Tempating or Container in custom Service?"