Last active
August 29, 2015 13:57
-
-
Save devjack/9701954 to your computer and use it in GitHub Desktop.
ZF2 traits example: UserServiceAwareTrait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SampleService\Controller; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use Zend\View\Model\ViewModel; | |
// Each 'AwareTrait' is expected to be configured by the controllers factory | |
use SampleService\Service\Feature\UserServiceAwareTrait; | |
use SampleService\Service\Feature\UserFilterServiceAwareTrait; | |
use SampleService\Form\Feature\FilterFormAwareTrait; | |
class UserController extends AbstractActionController | |
{ | |
use UserServiceAwareTrait; // a.k.a. This controller uses the UserService | |
use UserFilterServiceAwareTrait; | |
use FilterFormAwareTrait; | |
// Some action methods go here | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SampleService\Controller\Service; | |
use Zend\ServiceManager\FactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
use SampleService\Controller\Usercontroller; | |
class UserControllerFactory implements FactoryInterface | |
{ | |
public function createService(ServiceLocatorInterface $serviceLocator) | |
{ | |
/** @var ServiceLocatorInterface $serviceLocator */ | |
$serviceLocator = $serviceLocator->getServiceLocator(); | |
$controller = new UserController(); | |
// The factory is responsible for initialising (via the SL) | |
// each of the controllers dependencies. | |
$controller->setFilterForm( | |
$serviceLocator->get('FormElementManager')->get('SampleService\Form\FilterForm') | |
); | |
$controller->setUserService( | |
$serviceLocator->get('SampleService\Service\UserService') | |
); | |
$controller->setUserFilterService( | |
$serviceLocator->get('SampleService\Service\UserFilterService') | |
); | |
return $controller; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SampleService\Service\Feature; | |
use SampleService\Service\UserService; | |
use Zend\Stdlib\Exception\LogicException; | |
// This trait exposes the UserService to any 'aware' class. | |
// Controllers will most commonly use this trait. | |
trait UserServiceAwareTrait { | |
/** | |
* @var UserService | |
*/ | |
protected $userService; | |
public function setUserService($userService) | |
{ | |
$this->userService = $userService; | |
} | |
public function getUserService() | |
{ | |
if(null === $this->userService) { | |
throw new LogicException("User service must be defined"); | |
} | |
return $this->userService; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment