What's the best place to define services?

I am playing with yii container. Actually I am defining services in


config/web.php

file:




$container = Yii::$container;

$container->set('my_service', '\app\services\ServiceName');



I can call services correctly form controllers:




class SiteController

    extends Controller

{

    public function actionIndex()

    {

        $value = Yii::$container->get('my_service')->getValue();


        return $this->render(

            'index', [

                'value' => $value,

            ]

        );

    }

}



where my service is




<?php


namespace app\services;


class ServiceName

{

    public function getValue()

    {

        return 42;

    }

}



You can do it in "laravel way":

(Advanced template fronted)

params.php


\Yii::$container->set("frontend\service\AService","frontend\service\impl\AServiceImpl");


namespace frontend\service;


AService.php

interface AService {

    public function random():array;   

}

AServiceImpl.php


namespace frontend\service\impl;


use frontend\service\AService;


class AServiceImpl implements AService {...}

SiteController.php


class SiteController extends Controller

{

    private $aServiceImpl;

     

    public function __construct($id, $module, $config = [], AService $aServiceImpl)

    {

       $this->aServiceImpl = $aServiceImpl;

       parent::__construct($id, $module, $config);

    }