国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

修改頁面

有時(shí)候我們要修改頁面內(nèi)容或者樣式,這種情況下我們可以通過中間件或者路由覆蓋的方式來實(shí)現(xiàn)。

中間件更改頁面內(nèi)容

假設(shè)我們需要給登錄頁面添加一個(gè)邀請碼輸入框
img.png

創(chuàng)建插件foo

如果已經(jīng)創(chuàng)建則忽略

php webman app-plugin:create foo

創(chuàng)建中間件

plugin/foo/app/middleware/InviteCode.php

<?php
namespace plugin\foo\app\middleware;

use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;

class InviteCode implements MiddlewareInterface
{
    public function process(Request $request, callable $next) : Response
    {
        $response = $next($request);
        $body = $response->rawBody();
        $replacement = <<<HTML
                <div class="form-group">
                    <input type="password" name="password" class="form-control" placeholder="密碼" required>
                </div>
                <div class="form-group">
                    <input type="password" name="invite_code" class="form-control" placeholder="邀請碼" required>
                </div>
HTML;
        $body = preg_replace('/<div class="form-group">\r\n *<input *type="password" .*?>\r\n *<\/div>/', $replacement, $body);
        return $response->withBody($body);
    }
}

配置路由

打開路由文件plugin/foo/config/route.php給登錄頁面添加上面的中間件

<?php

use Webman\Route;
use plugin\foo\app\middleware\InviteCode;
use plugin\ai\app\controller\UserController;

Route::any('/app/ai/user/login', [UserController::class, 'login'])->middleware(InviteCode::class);

中間件介入控制器業(yè)務(wù)邏輯

類似的方法,開發(fā)者也可以通過中間件來接收invite_code參數(shù),然后做相應(yīng)的業(yè)務(wù)處理。

創(chuàng)建中間件

plugin/foo/app/middleware/InviteCodeCheck.php

<?php
namespace plugin\foo\app\middleware;

use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;

class InviteCodeCheck implements MiddlewareInterface
{
    public function process(Request $request, callable $next) : Response
    {
        $invite_code = $request->post('invite_code');
        if ($invite_code !== '123456') {
            return json(['code' => 1, 'msg' => '邀請碼錯(cuò)誤']);
        }
        return $next($request);
    }
}

配置路由

plugin/foo/config/route.php 文件增加配置

<?php

use Webman\Route;
use plugin\foo\app\middleware\InviteCodeCheck;
use plugin\user\app\controller\LoginController;
use plugin\foo\app\middleware\InviteCode;
use plugin\ai\app\controller\UserController;

Route::any('/app/ai/user/login', [UserController::class, 'login'])->middleware(InviteCode::class);
Route::any('/app/user/login', [LoginController::class, 'index'])->middleware(InviteCodeCheck::class);

提示
AI登錄提交接口復(fù)用的用戶模塊的登錄,接口地址是/app/user/login,并不在AI的目錄里。

以上通過中間件的方式,實(shí)現(xiàn)更改頁面內(nèi)容并增加了邀請碼驗(yàn)證功能。

中間件給所有頁面添加樣式文件或js文件

有時(shí)候我們想給AI所有的頁面都加載一個(gè)自己的css樣式或js文件,請參考以下代碼

新建中間件

plugin/foo/app/middleware/insertCss.php

<?php
namespace plugin\foo\app\middleware;

use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;

class insertCss implements MiddlewareInterface
{
    public function process(Request $request, callable $next) : Response
    {
        $response = $next($request);
        $body = $response->rawBody();
        $replacement = <<<HTML
    <link href="/app/foo/css/test.css" rel="stylesheet">
</head>
HTML;
        $body = preg_replace('/<\/head>/', $replacement, $body, 1);
        return $response->withBody($body);
    }
}

配置中間件

plugin/foo/config/middleware.php 文件增加配置

<?php

use plugin\foo\app\middleware\insertCss;

return [
    'plugin.ai' => [
        insertCss::class
    ]
];

注意
此用法需要 webman-framework >= v1.5.16,使用命令 php webman version 查看版本號
如需升級請執(zhí)行 composer update workerman/webman-framework

路由覆蓋重寫

有時(shí)候我們想徹底重寫某個(gè)功能頁面,這種情況下我們可以通過路由覆蓋的方式來實(shí)現(xiàn)。
例如新建一個(gè)登錄控制器plugin/foo/app/controller/LoginController.php

通過設(shè)置路由plugin/foo/config/route.php文件將登錄控制器替換成自己的控制器

use plugin\foo\app\controller\LoginController;
Route::any('/app/ai/user/login', [LoginController::class, 'login']);

通過事件監(jiān)聽實(shí)現(xiàn)業(yè)務(wù)邏輯

參考 添加圖標(biāo)菜單 一節(jié)

編輯于2024-04-25 15:47:24 完善本頁 +發(fā)起討論
贊助商