简体   繁体   English

如何让ZF2 Web服务在路由匹配上运行功能

[英]How to have a ZF2 web service run a function on route match

I've got a REST web service set up using ZF2, and what I want to do is have my web service call an intervening function before actually running the action associated to the route that was matched. 我已经使用ZF2设置了REST Web服务,而我想要做的是在实际运行与匹配的路由相关联的操作之前,让我的Web服务调用一个干预函数。

To be a bit more specific, but I want to do is, any time a route is matched for my module, to have a function run that inspects a value in the request header, and then either allows the action to be called or returns an error code. 更具体一点,但是我想做的是,只要我的模块匹配了路由,就运行一个函数来检查请求标头中的值,然后允许调用该操作或返回一个错误代码。

For example, if I have the following route in my module.config.php : 例如,如果我在module.config.php具有以下路由:

'TestCall' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/Rest/TestCall[/]',
        'defaults' => array(
            'controller' => 'Rest\Controller\Rest',
            'action' => 'TestCall',
        ),
    ),
),

And then, in RestController.php I have the following: 然后,在RestController.php我有以下内容:

public function TestCallAction()
{
    return new JsonModel(array("I worked!"));
}

Whenever the route TestCall is matched, I would like to, before the action is fired , fire the following: 每当路由TestCall匹配时,我希望在触发操作之前触发以下操作:

public function FireCheck()
{
    if ($this->getRequest()->getHeaders()->get('customheadervalue')->getFieldValue() == 'bananas')
    {
        //now fire the actual action
    }
    else
    {
        $this->getResponse()->setStatusCode(403);
        return new JsonModel(array("Nope, not allowed!"));
    }
}

Is this possible? 这可能吗? Is there some way I can accomplish this? 有什么办法可以做到这一点? I'm pretty new to both Zend Framework and to PHP. 我对Zend Framework和PHP都是新手。

Thanks in advance for any help. 在此先感谢您的帮助。

For the record, I managed to accomplish using information found here . 作为记录,我设法使用此处找到的信息来完成。

What I did was to add the following to my controller: 我所做的就是将以下内容添加到我的控制器中:

public function setEventManager(EventManagerInterface $events)
{
    parent::setEventManager($events);
    $controller = $this;
    $events->attach('dispatch', function ($e) use ($controller) {
        $request = $e->getRequest();
        $token = $request->getHeaders()->get('accesstoken')->getFieldValue();        

        if (!$token == "allowed")
        {
            return $controller->redirect()->toRoute('AccessDenied');
        }

        return;
    }, 100);
}

As well as adding this use statement to my controller: 以及将此use语句添加到我的控制器中:

use Zend\EventManager\EventManagerInterface;

This is binding a function to the dispatch event, which is fired, as far as I'm aware, after the request is received but before the route is actually processed. 据我所知,这是将一个函数绑定到dispatch事件,据我所知,该事件是在收到请求之后但在实际处理路由之前触发的。 If the verification condition fails, the request is redirected to another route, otherwise the request is allowed to follow through to the expected route. 如果验证条件失败,则将请求重定向到另一条路由,否则允许该请求继续到期望的路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM