Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
public function doCreate($rName, $cName) {
// ..
foreach ($this->initializers as $initializer) {
if ($initializer instanceof InitializerInterface) {
$initializer->initialize($instance, $this);
} else {
call_user_func($initializer, $instance, $this);
}
}
}
Хорошей практикой является добавления неймспейса модуля к названию сервиса. Либо использовать абсолютное название класса сервиса.
// В конфиге:
namespace User;
....
'service_manager' => [
'factories' => [
Service\RoleService::class => Factory\Service\RoleServiceFactory::class,
],
],
//В контроллере:
use User\Service\RoleService;
......
public function indexAction() {
//При вводе Ro -> выскакивает автодополнение IDE для класа RoleService
// А в PhpStorm даже без указания use вверху (потом после ввода класа шторм сам добавит нужный use)
$roleService = $this->getServiceLocator()->get(RoleService::class);
// А для автодополнения $roleService приходиться указывать PhpDoc @var
}
use Library\Mvc\Controller;
/**
* @property \Library\Acl $acl
* @property \Library\Session $session
* @property \Library\Mvc\Request $request
*/
abstract class ControllerAbstract extends YourFrameworkControllerAbstract {
}
/**
* @property \Library\Acl $acl
* @property \Library\Session $session
* @property \Library\Mvc\Request $request
*/
interface ServicesDummy {
}
abstract class ControllerAbstract extends YourFrameworkControllerAbstract implements ServicesDummy {
}
/**
* @return \Library\Acl
*/
protected function getAclService(){}
//...
/**
* Abstract controller
*
* Convenience methods for pre-built plugins (@see __call):
*
* [....]
* @method mixed|null identity()
* @method \Zend\Http\Response|array prg(string $redirect = null, bool $redirectToUrl = false)
* @method \Zend\Http\Response|array postRedirectGet(string $redirect = null, bool $redirectToUrl = false)
* @method \Zend\Mvc\Controller\Plugin\Redirect redirect()
* @method \Zend\Mvc\Controller\Plugin\Url url()
*/
abstract class AbstractController
/* @var $user User */
$user = $sm->get('User');
if (!file_exists(__DIR__ . '/di-definition.php')) {
$compiler = new Zend\Di\Definition\Compiler();
$compiler->addCodeScannerDirectory(
new Zend\Code\Scanner\ScannerDirectory('path/to/library/My/')
);
$definition = $compiler->compile();
file_put_contents(
__DIR__ . '/di-definition.php',
'<?php return ' . var_export($definition->toArray(), true) . '?>;'
);
} else {
$definition = new Zend\Di\Definition\ArrayDefinition(
include __DIR__
span style= . '/di-definition.php'
);
}
'index_navigation' => function (\Zend\ServiceManager\ServiceManager $sm) {
$navigationF = new Navigation\Service\IndexNavidationFactory();
$navigation = $navigationF->createService($sm);
return $navigation;
},
'index_navigation' => new Navigation\Service\IndexNavidationFactory(),
'index_navigation' => 'Application\Navigation\Service\IndexNavigationFactory'
СМ реалирует
Zend Framework 2: Service Manager