खोज…


सिम्फनी 2 में एक साधारण सेवा की घोषणा, लेखन और उपयोग कैसे करें

सेवाओं की घोषणा:

# src/Acme/YourBundle/Resources/config/services.yml

services:
    my_service:
        class: Acme\YourBundle\Service\MyService
        arguments: ["@doctrine", "%some_parameter%", "@another_service"]
    another_service:
        class: Acme\YourBundle\Service\AnotherService
        arguments: []

सेवा कोड:

<?php
namespace Acme\YourBundle\Service\Service;

class MyService 
{
    /**
     * Constructor
     * You can had whatever you want to use in your service by dependency injection
     * @param $doctrine Doctrine
     * @param $some_parameter Some parameter defined in app/config/parameters.yml
     * @param $another_service Another service
     */
    public function __construct($doctrine, $some_parameter, $another_service) 
    {
        $this->doctrine = $doctrine;
        $this->some_parameter = $some_parameter;
        $this->another_service = $another_service;
    }

    public function doMagic() 
    {
        // Your code here
    }
}

नियंत्रक में इसका उपयोग करें:

<?php

namespace Acme\YourBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\YourBundle\Service\Service\MyService;

class MyController extends Controller
{
  /**
     * One action
     */
    public function oneAction(Request $request)
    {
        $myService = $this->get('my_service');
        $myService->doMagic();
        // ...
    }
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow