Created
September 25, 2012 18:45
-
-
Save weierophinney/3783654 to your computer and use it in GitHub Desktop.
Abstract controller factory
This file contains hidden or 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
| namespace My\Api\V1; | |
| use Zend\ServiceManager\AbstractFactoryInterface; | |
| use Zend\ServiceManager\ServiceLocatorInterface; | |
| class AbstractControllerFactory implements AbstractFactoryInterface | |
| { | |
| protected $allowedControllers = array( | |
| 'user', | |
| 'group', | |
| 'etc', | |
| ); | |
| public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName) | |
| { | |
| if (0 !== strpos($name, 'apiv1controller')) { | |
| return false; | |
| } | |
| $name = substr($name, 15); | |
| if (in_array($cName, $allowedControllers)) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName) | |
| { | |
| $name = substr($name, 15); | |
| if (in_array($cName, $allowedControllers)) { | |
| throw new \RuntimeException(sprintf('Unrecognized controller %s', $name)); | |
| } | |
| $controller = __NAMESPACE__ . '\\' . ucfirst($name) . 'Controller'; | |
| return new $controller; | |
| } | |
| } |
This file contains hidden or 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 | |
| return array( | |
| 'router' => array('routes' => array( | |
| 'api' => array( | |
| 'type' => 'Segment', | |
| 'options' => array( | |
| 'route' => '/api/v1/:controller[/:id]', | |
| 'defaults' => array( | |
| '__NAMESPACE__' => 'Api\V1\Controller', | |
| 'controller' => 'User', // just an example | |
| ), | |
| ), | |
| ), | |
| )), | |
| 'controllers' => array( | |
| 'abstract_factories' => array( | |
| 'My\Api\V1\AbstractControllerFactory', | |
| ), | |
| ), | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment