2017 © Pedro Peláez
 

library x-phalcon-middleware

a phalcon middleware component

image

limingxinleo/x-phalcon-middleware

a phalcon middleware component

  • Saturday, March 3, 2018
  • by limingxinleo
  • Repository
  • 1 Watchers
  • 6 Stars
  • 2,298 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 39 % Grown

The README.md

x-phalcon-middleware

a phalcon middleware component, (*1)

Build Status, (*2)

感谢

https://github.com/shouhuyou/phalcon-midddleware.git, (*3)

安装

composer require limingxinleo/x-phalcon-middleware, (*4)

配置

添加服务, (*5)

<?php

use Xin\Phalcon\Middleware\Mvc\Dispatcher as MvcDispatcher;
use Xin\Phalcon\Middleware\Mvc\Dispatcher71 as MvcDispatcher71;

$di->setShared('middlewareManager', function () {
    $middlewareManager = new Manager();
    //注册中间件
    $middlewareManager->add('test', \Tests\App\Middleware\Test1Middleware::class);
    $middlewareManager->add('test2', \Tests\App\Middleware\Test2Middleware::class);
    $middlewareManager->add('test3', \Tests\App\Middleware\Test3Middleware::class);

    return $middlewareManager;
});

//替换默认的dispatcher
$di->setShared('dispatcher', function () {

    if (version_compare(PHP_VERSION, '7.1', '>')) {
        $dispatcher = new MvcDispatcher71();
    } else {
        $dispatcher = new MvcDispatcher();
    }
    $dispatcher->setDefaultNamespace('Tests\App\Controllers');

    return $dispatcher;
});

使用

  • 创建中间件
<?php

namespace App\Middleware;

use Xin\Phalcon\Middleware\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;
  }
}
  • 控制器中使用中间件
<?php
namespace Tests\App\Controllers;

use Phalcon\Mvc\Controller;

class IndexController extends Controller
{
    public function indexAction()
    {
        return $this->response->setJsonContent([
            'success' => true,
            'data' => ['action' => 'index']
        ]);
    }

    /**
     * @desc
     * @author limx
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     * @Middleware('test')
     */
    public function testAction()
    {
        return $this->response->setJsonContent([
            'success' => true,
            'data' => ['action' => 'test']
        ]);
    }

    /**
     * @desc
     * @author limx
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     * @Middleware('test2')
     */
    public function test2Action()
    {
        return $this->response->setJsonContent([
            'success' => true,
            'data' => ['action' => 'test2']
        ]);
    }

    /**
     * @desc
     * @author limx
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     * @Middleware('test2')
     * @Middleware('test3')
     */
    public function test3Action()
    {
        return $this->response->setJsonContent([
            'success' => true,
            'data' => ['action' => 'test2']
        ]);
    }
}

中间件执行顺序

中间件定义, (*6)

class OneMiddleware extends Middleware
{
    public function handle($request, Closure $next)
    {
        echo 'One1' . PHP_EOL;
        $response = $next($request);
        echo 'One2' . PHP_EOL;
        return $response;
    }
}

class TwoMiddleware extends Middleware
{
    public function handle($request, Closure $next)
    {
        echo 'Two1' . PHP_EOL;
        $response = $next($request);
        echo 'Two2' . PHP_EOL;
        return $response;
    }
}

class ThreeMiddleware extends Middleware
{
    public function handle($request, Closure $next)
    {
        echo 'Three1' . PHP_EOL;
        $response = $next($request);
        echo 'Three2' . PHP_EOL;
        return $response;
    }
}

class FourMiddleware extends Middleware
{
    public function handle($request, Closure $next)
    {
        echo 'Four1' . PHP_EOL;
        $response = $next($request);
        echo 'Four2' . PHP_EOL;
        return $response;
    }
}

使用如下, (*7)

/**
 * Class IndexController
 * @package Tests\App\Controllers
 * @Middleware('sort1')
 */
class IndexController extends Controller
{
    public function initialize()
    {
        $this->middleware->set([
            'abs',
            'test3'
        ]);
    }

    /**
     * @Middleware('sort2')
     * @Middleware('sort3')
     */
    public function indexAction()
    {
        return $this->response->setJsonContent([
            'success' => true,
            'data' => ['action' => 'index']
        ]);
    }
}

结果, (*8)

'abs.start',
'test3.start',
'sort1.start',
'sort2.start',
'sort3.start',
'sort3.end',
'sort2.end',
'sort1.end',
'test3.end',
'abs.end',

The Versions

03/03 2018

dev-master

9999999-dev

a phalcon middleware component

  Sources   Download

MIT

The Requires

  • ext-phalcon ^3.0
  • php >=5.5

 

The Development Requires

by Avatar limingxinleo
by liaoliang

phalcon phalcon-middleware

03/03 2018

1.1.1

1.1.1.0

a phalcon middleware component

  Sources   Download

MIT

The Requires

  • php >=5.5
  • ext-phalcon ^3.0

 

The Development Requires

by Avatar limingxinleo
by liaoliang

phalcon phalcon-middleware

22/01 2018

1.1.0

1.1.0.0

a phalcon middleware component

  Sources   Download

MIT

The Requires

  • php >=5.6
  • ext-phalcon ^3.0

 

The Development Requires

by Avatar limingxinleo
by liaoliang

phalcon phalcon-middleware

04/01 2018

1.0.0

1.0.0.0

a phalcon middleware component

  Sources   Download

MIT

The Requires

  • php >=5.6
  • ext-phalcon ^3.0

 

The Development Requires

by Avatar limingxinleo
by liaoliang

phalcon phalcon-middleware

08/11 2017

0.1.4

0.1.4.0

a phalcon middleware component

  Sources   Download

MIT

The Requires

  • php >=5.6
  • ext-phalcon ^3.0

 

The Development Requires

by Avatar limingxinleo
by liaoliang

phalcon phalcon-middleware

03/11 2017

0.1.3

0.1.3.0

a phalcon middleware component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar limingxinleo
by liaoliang

phalcon phalcon-middleware

02/11 2017

0.1.2

0.1.2.0

a phalcon middleware component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar limingxinleo
by liaoliang

phalcon phalcon-middleware

04/05 2017

dev-shy

dev-shy

a phalcon middleware component

  Sources   Download

MIT

by liaoliang

phalcon phalcon-middleware