Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Created September 25, 2012 18:45
Show Gist options
  • Select an option

  • Save weierophinney/3783654 to your computer and use it in GitHub Desktop.

Select an option

Save weierophinney/3783654 to your computer and use it in GitHub Desktop.
Abstract controller factory
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;
}
}
<?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