All services can be accessed by FQCN.
This project can be installed via Composer:
composer require symfonette/class-named-services
use Symfonette\ClassNamedServices\ContainerBuilderConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
$builder = new ContainerBuilder();
$configurator = new ContainerBuilderConfigurator();
$configurator->configure($builder);
use Symfonette\ClassNamedServices\Bundle\ClassNamedServiceBundle;
use Symfony\Component\HttpKernel\Kernel;
class AppKernel extends Kernel
{
public function registerBundles()
{
return [
// ...
new ClassNamedServiceBundle,
// ...
];
}
}
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);
// ...
}
}