您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
webman-多應用-多应用異常處理配置
发布时间:2022-01-29 23:20:15编辑:雪饮阅读()
同樣是基於上篇中多應用中間件配置來完成的。
先看看效果
服務端運行實例被訪問的效果:
/www/wwwroot/webman/webman/app/controller/Index.php update and reload
Workerman[start.php] reloading
訪問了全局中間件!
訪問了全局中間件!
訪問了api中間件!
--------進入report了--------------進入render了------訪問了全局中間件!
訪問了admin中間件001!
訪問了admin中間件002!
--------admin exception 進入report了--------------admin exception進入render了------
這裏其實有用到3個異常處理,并且有3次請求,每次請求不同應用,那麽這裏只顯示了兩次不同的report(這裏對應的是兩個不同的exception),而另外沒有顯示的exception則是配置的内置的exception,那個exception並沒有在運行實例処進行輸出,而是僅僅在響應給訪問者。
那麽訪問者訪問流程順序及對應響應如:
[root@localhost ~]# elinks http://127.0.0.1:8787/Index/index --dump
Exception: 抛出异常 from global in
/www/wwwroot/webman/webman/app/controller/Index.php:10
Stack trace:
#0
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(229):
app\controller\Index->index(Object(support\Request))
#1 /www/wwwroot/webman/webman/app/middleware/AuthCheck.php(11):
Webman\App::Webman\{closure}(Object(support\Request))
#2
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(223):
app\middleware\AuthCheck->process(Object(support\Request),
Object(Closure))
#3
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(157):
Webman\App::Webman\{closure}(Object(support\Request))
#4
/www/wwwroot/webman/webman/vendor/workerman/workerman/Connection/TcpConnection.php(638):
Webman\App->onMessage(Object(Workerman\Connection\TcpConnection),
Object(support\Request))
#5
/www/wwwroot/webman/webman/vendor/workerman/workerman/Events/Select.php(294):
Workerman\Connection\TcpConnection->baseRead(Resource id #144)
#6 /www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(2418):
Workerman\Events\Select->loop()
#7 /www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1542):
Workerman\Worker->run()
#8 /www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1373):
Workerman\Worker::forkOneWorkerForLinux(Object(Workerman\Worker))
#9 /www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1347):
Workerman\Worker::forkWorkersForLinux()
#10
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1679):
Workerman\Worker::forkWorkers()
#11
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1628):
Workerman\Worker::monitorWorkersForLinux()
#12 /www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(544):
Workerman\Worker::monitorWorkers()
#13 /www/wwwroot/webman/webman/start.php(87): Workerman\Worker::runAll()
#14 {main}
[root@localhost ~]# elinks http://127.0.0.1:8787/api/Index/index --dump
Exception: 抛出异常 from api in
/www/wwwroot/webman/webman/app/api/controller/Index.php:10
Stack trace:
#0
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(229):
app\api\controller\Index->index(Object(support\Request))
#1 /www/wwwroot/webman/webman/app/middleware/AccessControl.php(11):
Webman\App::Webman\{closure}(Object(support\Request))
#2
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(223):
app\middleware\AccessControl->process(Object(support\Request),
Object(Closure))
#3 /www/wwwroot/webman/webman/app/middleware/AuthCheck.php(11):
Webman\App::Webman\{closure}(Object(support\Request))
#4
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(223):
app\middleware\AuthCheck->process(Object(support\Request),
Object(Closure))
#5
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(157):
Webman\App::Webman\{closure}(Object(support\Request))
#6
/www/wwwroot/webman/webman/vendor/workerman/workerman/Connection/TcpConnection.php(638):
Webman\App->onMessage(Object(Workerman\Connection\TcpConnection),
Object(support\Request))
#7
/www/wwwroot/webman/webman/vendor/workerman/workerman/Events/Select.php(294):
Workerman\Connection\TcpConnection->baseRead(Resource id #166)
#8 /www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(2418):
Workerman\Events\Select->loop()
#9 /www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1542):
Workerman\Worker->run()
#10
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1373):
Workerman\Worker::forkOneWorkerForLinux(Object(Workerman\Worker))
#11
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1347):
Workerman\Worker::forkWorkersForLinux()
#12
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1679):
Workerman\Worker::forkWorkers()
#13
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1628):
Workerman\Worker::monitorWorkersForLinux()
#14 /www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(544):
Workerman\Worker::monitorWorkers()
#15 /www/wwwroot/webman/webman/start.php(87): Workerman\Worker::runAll()
#16 {main}
[root@localhost ~]# elinks http://127.0.0.1:8787/admin/Index/index --dump
Exception: 抛出异常 from admin in
/www/wwwroot/webman/webman/app/admin/controller/Index.php:10
Stack trace:
#0
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(229):
app\admin\controller\Index->index(Object(support\Request))
#1 /www/wwwroot/webman/webman/app/middleware/SomeOtherClass.php(11):
Webman\App::Webman\{closure}(Object(support\Request))
#2
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(223):
app\middleware\SomeOtherClass->process(Object(support\Request),
Object(Closure))
#3 /www/wwwroot/webman/webman/app/middleware/AdminAuthCheck.php(11):
Webman\App::Webman\{closure}(Object(support\Request))
#4
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(223):
app\middleware\AdminAuthCheck->process(Object(support\Request),
Object(Closure))
#5 /www/wwwroot/webman/webman/app/middleware/AuthCheck.php(11):
Webman\App::Webman\{closure}(Object(support\Request))
#6
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(223):
app\middleware\AuthCheck->process(Object(support\Request),
Object(Closure))
#7
/www/wwwroot/webman/webman/vendor/workerman/webman-framework/src/App.php(157):
Webman\App::Webman\{closure}(Object(support\Request))
#8
/www/wwwroot/webman/webman/vendor/workerman/workerman/Connection/TcpConnection.php(638):
Webman\App->onMessage(Object(Workerman\Connection\TcpConnection),
Object(support\Request))
#9
/www/wwwroot/webman/webman/vendor/workerman/workerman/Events/Select.php(294):
Workerman\Connection\TcpConnection->baseRead(Resource id #169)
#10
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(2418):
Workerman\Events\Select->loop()
#11
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1542):
Workerman\Worker->run()
#12
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1373):
Workerman\Worker::forkOneWorkerForLinux(Object(Workerman\Worker))
#13
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1347):
Workerman\Worker::forkWorkersForLinux()
#14
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1679):
Workerman\Worker::forkWorkers()
#15
/www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(1628):
Workerman\Worker::monitorWorkersForLinux()
#16 /www/wwwroot/webman/webman/vendor/workerman/workerman/Worker.php(544):
Workerman\Worker::monitorWorkers()
#17 /www/wwwroot/webman/webman/start.php(87): Workerman\Worker::runAll()
#18 {main}
接下來查看具體搭建流程:
創建兩個異常(因爲有一個内置異常,我們總共3個應用,那個默認應用也就將就算一個了)
在app/admin/創建exception/Handler.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
*/
namespace app\admin\exception;
use Webman\Http\Request;
use Webman\Http\Response;
use Throwable;
use Webman\Exception\ExceptionHandler;
/**
* Class Handler
* @package support\exception
*/
class Handler extends ExceptionHandler
{
public $dontReport = [
BusinessException::class,
];
public function report(Throwable $exception)
{
echo "--------admin exception 進入report了------";
parent::report($exception);
}
public function render(Request $request, Throwable $exception) : Response
{
echo "--------admin exception進入render了------";
return parent::render($request, $exception);
}
}
在app/api/創建exception/Handler.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
*/
namespace app\api\exception;
use Webman\Http\Request;
use Webman\Http\Response;
use Throwable;
use Webman\Exception\ExceptionHandler;
/**
* Class Handler
* @package support\exception
*/
class Handler extends ExceptionHandler
{
public $dontReport = [
BusinessException::class,
];
public function report(Throwable $exception)
{
echo "--------進入report了------";
parent::report($exception);
}
public function render(Request $request, Throwable $exception) : Response
{
echo "--------進入render了------";
return parent::render($request, $exception);
}
}
然後三個控制器中分別抛出不同異常用以在訪問者訪問時結合該多應用實例運行時的輸出進行區分:
app/admin/controller/Index.php:
<?php
namespace app\admin\controller;
use support\Request;
class Index
{
public function index(Request $request)
{
throw new \Exception("抛出异常 from admin");
return response('this is admin index');
}
public function view(Request $request)
{
return view('index/view', ['name' => 'webman']);
}
public function json(Request $request)
{
return json(['code' => 0, 'msg' => 'ok']);
}
}
app/api/controller/Index.php:
<?php
namespace app\api\controller;
use support\Request;
class Index
{
public function index(Request $request)
{
throw new \Exception("抛出异常 from api");
return response('this is api index');
}
public function view(Request $request)
{
return view('index/view', ['name' => 'webman']);
}
public function json(Request $request)
{
return json(['code' => 0, 'msg' => 'ok']);
}
}
app/controller/Index.php:
<?php
namespace app\controller;
use support\Request;
class Index
{
public function index(Request $request)
{
throw new \Exception("抛出异常 from global");
return response('this is index');
}
public function view(Request $request)
{
return view('index/view', ['name' => 'webman']);
}
public function json(Request $request)
{
return json(['code' => 0, 'msg' => 'ok']);
}
}
接下來在config/exception.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
*/
use support\exception\Handler;
return [
'' => support\exception\Handler::class,
'api' => app\api\exception\Handler::class,
'admin' => app\admin\exception\Handler::class,
];
這裏第一個異常配置有點”怪異“,而我們的多應用目前也就是app/controller/Index.php這個控制器沒有顯式説明是什麽應用,或許這樣也正好一一對應吧,儅訪問
http://127.0.0.1:8787/Index/index時候正好走的是這裏第一個異常配置項,而訪問api或admin應用時候也僅僅走了對應api或admin的異常配置項,並沒有比如在走api或admin異常配置項之前先走下這裏第一個配置項,也就是
將第一個異常配置項理解為全局異常配置項,實際不然,僅僅只是匹配了這裏的http://127.0.0.1:8787/Index/index這個路由,這裏僅僅是在下親測,如有不到之處,敬請原諒。
关键字词:webman,多應用,異常,處理,配置
下一篇:webman-數據庫-快速開始