juicelib / filtermodule
在路由/MvcEvent::EVENT_ROUTE 事件中拦截请求/响应,并转换或使用请求或响应
dev-master
2015-03-23 03:13 UTC
Requires
- php: >=5.3.23
- zendframework/zend-mvc: >=2.3.7
This package is not auto-updated.
Last update: 2024-09-28 17:33:15 UTC
README
在 route
/MvcEvent::EVENT_ROUTE
事件中拦截请求/响应,并转换或使用请求或响应。
用法
在 application.config.php
中包含 JuiceLib\FilterModule
'modules' => array(
'JuiceLib\FilterModule',
// your other modules
),
在 config/autoload
内创建一个 juice.global.php
文件。或者你也可以在你的模块的 module.config.php
中添加过滤器配置
<?php
// juice.global.php
return array(
'juice' => array(
'filter' => array(
'FilterProtection' => array(
'/protected/(.*)',
),
),
'filter_mapping' => array(
'FilterProtection' => 'Protected\Filter\Namespace\MyFilter',
),
),
);
创建你的过滤器类,这里我将其命名为 MyFilter.php
,它必须与配置中的过滤器映射相匹配。
<?php
namespace Protected\Filter\Namespace;
use JuiceLib\FilterModule\Filter\FilterInterface;
use Zend\Http\Request as HttpRequest;
use Zend\Http\Response as HttpResponse;
use Zend\Mvc\Application;
class MyFilter implements FilterInterface {
public function processFilter(HttpRequest &$request, HttpResponse &$response, Application &$application)
{
/** @var \Zend\Http\Header\HeaderInterface $authKeyHeader */
$authKeyHeader = $request->getHeader('my-auth-key');
if (!$authKeyHeader || $authKeyHeader->getFieldValue() != 'secret-password') {
$response->setStatusCode(401);
return $response;
}
}
}
在上一个例子中,我们通过检查 my-auth-key
头来保护以 /protected/
开头的任何路径。这也可以与会话或其他认证方法一起使用。
此外,这也可以用来为匹配过滤器路径的每个从应用程序发送的响应添加你自己的响应头。