PHP code example of myph / phalcon-middleware
1. Go to this page and download the library: Download myph/phalcon-middleware library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
myph / phalcon-middleware example snippets
$di->setShared('middlewareManager',function(){
$middlewareManager = new \Myph\Middleware\Manager();
//注册中间件
$middlewareManager->add('middlewareAlias','middlewareHandle');
// ...
return $middleware
});
//替换默认的dispatcher
$di->setShared('dispatcher',\Myph\Dispatcher::class);
namespace App\Middleware;
use Myph\Middleware;
class Aaa extends Middleware
{
public function handle($request ,\Closure $next)
{
//在中间件中使用service
if($this->request->isAjax()){
return $this->response->redirect('/login');
}
//前置操作
echo __METHOD__.'<hr>';
$response = $next($request);
//后置操作
echo __METHOD__.'<hr>';
return $response;
}
}
$middlewareManager->add('middlewareAlias1',App\Middleware\Aaa::class);
$middlewareManager->add('middlewareAlias2','middlewareHandle2');
$middlewareManager->add('middlewareAlias3','middlewareHandle3');
public function initialize()
{
$this->middleware->set(
['csrf','auth'],
[
//这里可以使用only和except,对应的参数是方法名
'except'=>['login','register']
]
);
}
namespace App\Controllers;
/**
* @Middleware('bbb');
*/
class IndexController extends ControllerBase
{
/**
* @Middleware('aaa','csrf','auth')
*/
public function index()
{
echo __METHOD__.'<hr>';
}
}
$di->setShared('middlewareManager',function(){
$middlewareManager = new \Myph\Middleware\Manger();