Home » Php » php – How to configure services.yaml when i have submodules?

php – How to configure services.yaml when i have submodules?

Posted by: admin February 25, 2020 Leave a comment

Questions:

i’m just trying to create a subdirectory structure for my system, i’ve already readed the documentation, courses and youtube vids, but nothing works, every time i got the error anytime i add something

enter image description here

my Directory Structure

src -> Base -> Offer -> src -> Offer or Users -> repository -> UsersRepository.php

src -> Entity -> UserEntity.php

src -> Admin -> Offer -> src -> Offer or Users -> controller -> OfferController.php

src -> Admin -> Offer -> src -> Offer or Users -> service -> OfferService.php

#services.yaml 

# my controller
App\Offer\Offer\Controller\:
    resource: '../src/Web/Offer/src/Offer/Controller'
    tags: ['controller.service_arguments']

# default service
offer.service:
    class: App\Offer\Offer\Service\OfferService
    public: true
App\Offer\Offer\Service\OfferService: '@offer.service'

# default repository
offer.repo:
    class: App\Base\Offer\Offer\Repository\UsersRepository
    public: true

App\Base\Offer\Offer\Repository\UsersRepository: '@offer.repo'

in my controller

<?php
namespace App\Offer\Offer\Controller;
use App\Base\Offer\Offer\Repository\UsersRepository;
use App\Offer\Offer\Service\OfferService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

class OfferController extends AbstractController
{

    /**
     * @Route("/web/offer", name="offer_list")
     */
    public function index(OfferService $service,UsersRepository $repo)
    {
        # can i do this ?
        #$a = new OfferService();
        #$a = $this->get('offer.service');

        $b = $service->OfferTester();

        $c = $repo->findAll();

        return new Response(
           '{"success":"'.$c.'"}'
        );
    }
}

in my repo

namespace App\Base\Offer\Offer\Repository;

use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @method User|null find($id, $lockMode = null, $lockVersion = null)
 * @method User|null findOneBy(array $criteria, array $orderBy = null)
 * @method User[]    findAll()
 * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */

class UsersRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, User::class);
    }


}

has any way to keep it simple or dynamic ? im almost giving up, thanks in advice 🙂

How to&Answers:

If you need to specify services configuration like you are doing you just missed class definition:

App\Base\Offer\Offer\Repository\UsersRepository:
    public: false

# default repository
offer.repo:
    class: App\Base\Offer\Offer\Repository\UsersRepository
    public: true

But to make it clear, symfony preconize this way to configure services
https://symfony.com/doc/current/service_container.html#importing-many-services-at-once-with-resource :

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

PS: for symfony 3 or above.