IN ZF3 This is my Factory for a few controllers that will require user Authintication:
// Factory class
class ProjectControllerFactory implements FactoryInterface
{
private $msg;
public function __invoke(ContainerInterface $container, $requestedName, Array $options = null) {
$entityManager = $container->get('doctrine.entitymanager.orm_default');
$config = $container->get('config');
$request = $container->get('request');
if($this->authorize($request)){
return new $requestedName($entityManager, $config);
}
echo json_encode([
'success'=>false,
'msg'=> 'Your are not authorized to access this page'
]);exit;
}
private function authorize($request){... code here}
}
Now here authorize
is function that will return true or false based on user access, i just want to return Json
with status code or 401
if user want to access that page.
The question here is, am i doing it Right? is this the right way of checking user ?
if so how can i pass the return 401 code, i tried JsonModel()
instead of echo json_encode()
but it gives me the error:
Plugin of type "Zend\View\Model\JsonModel" is invalid; must implement Zend\Stdlib\DispatchableInterface