Skip to content

symfonette/class-named-services

Repository files navigation

Class Named Services

Packagist Build Status StyleCI Scrutinizer Code Quality SensioLabsInsight

All services can be accessed by FQCN.

Installation

This project can be installed via Composer:

composer require symfonette/class-named-services

Standalone

use Symfonette\ClassNamedServices\ContainerBuilderConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;

$builder = new ContainerBuilder();
$configurator = new ContainerBuilderConfigurator();

$configurator->configure($builder);

Symfony Bundle

use Symfonette\ClassNamedServices\Bundle\ClassNamedServiceBundle;
use Symfony\Component\HttpKernel\Kernel;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        return [
            // ...
            new ClassNamedServiceBundle,
            // ...
        ];
    }
}

Usage

In dependency injection definition:

# app/config/services.yml
services:
    controller_main:
        class: AppBundle\Controller\MainController
        arguments:
            - '@Symfony\Bridge\Doctrine\RegistryInterface'
            - '@Twig_Environment'

In routing definition:

# app/config/routing.yml
homepage:
    path: /
    defaults:
      _controller: AppBundle\Controller\MainController:homepage

In controller:

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class ContactController extends Controller
{
    public function formAction()
    {
        $validator = $this->get(Validator::class);
        // ...
    }
}