您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-多應用-多应用中间件配置
发布时间:2022-01-29 22:47:28编辑:雪饮阅读()
多應用中間件配置,這裏基於上篇中的多應用部署基礎上構建多應用中間件配置。
分別建立中間件:
app/middleware/AccessControl.php:
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class AccessControl implements MiddlewareInterface
{
public function process(Request $request, callable $next) : Response
{
echo "訪問了api中間件!\r\n";
return $next($request);
}
}
app/middleware/AdminAuthCheck.php:
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class AdminAuthCheck implements MiddlewareInterface
{
public function process(Request $request, callable $next) : Response
{
echo "訪問了admin中間件001!\r\n";
return $next($request);
}
}
app/middleware/AuthCheck.php:
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class AuthCheck implements MiddlewareInterface
{
public function process(Request $request, callable $next) : Response
{
echo "訪問了全局中間件!\r\n";
return $next($request);
}
}
app/middleware/SomeOtherClass.php:
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class SomeOtherClass implements MiddlewareInterface
{
public function process(Request $request, callable $next) : Response
{
echo "訪問了admin中間件002!\r\n";
return $next($request);
}
}
然後可以在config/middleware.php中將以上的中間件都應用到各個應用上:
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
return [
// 全局中间件
'' => [
app\middleware\AuthCheck::class,
],
// api应用中间件
'api' => [
app\middleware\AccessControl::class,
],
// admin应用中间件
'admin' => [
app\middleware\AdminAuthCheck::class,
app\middleware\SomeOtherClass::class,
],
];
當然也可以在路由分組上進行應用,但那樣是路由中間件而不是多應用中間件的使用方式。
該多應用項目再次啓動並分別從不同的子應用訪問時:
[root@localhost webman]# /usr/local/php734/bin/php -c /usr/local/php734/lib/php.ini start.php start
Workerman[start.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.26 PHP version:7.3.4
------------------------------------------ WORKERS ------------------------------------------
proto user worker listen processes status
tcp root webman http://0.0.0.0:8787 4 [OK]
tcp root monitor none 1 [OK]
---------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
訪問了全局中間件!
訪問了全局中間件!
訪問了admin中間件001!
訪問了admin中間件002!
訪問了全局中間件!
訪問了api中間件!
不同子應用訪問順序如:
[root@localhost ~]# elinks http://127.0.0.1:8787/Index/index --dump
this is index
[root@localhost ~]# elinks http://127.0.0.1:8787/admin/Index/index --dump
this is admin index
[root@localhost ~]# elinks http://127.0.0.1:8787/api/Index/index --dump
this is api index
关键字词:webman,多應用,中间件,配置
上一篇:webman-多應用-第二彈