webman 里面使用$_SERVER里面怎么什么参数都没有

luojunwei

问题描述

webman 里面使用$_SERVER里面怎么什么参数都没有

print_r($_SERVER);

输出参数很多都没有

Array
(
    [COMMAND_MODE] => unix2003
    [HOME] => /Users/junweiluo
    [__CF_USER_TEXT_ENCODING] => 0x1F5:0x19:0x34
    [LOGIN_SHELL] => 1
    [PATH] => /usr/local/mysql/bin:/usr/local/opt/php@7.4/sbin:/usr/local/opt/php@7.4/bin:/usr/local/mysql/bin:/usr/local/opt/php@7.4/sbin:/usr/local/opt/php@7.4/bin:/Users/junweiluo/opt/anaconda3/bin:/Users/junweiluo/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
    [USER] => junweiluo
    [SSH_AUTH_SOCK] => /private/tmp/com.apple.launchd.GlhUgQ8FEq/Listeners
    [__INTELLIJ_COMMAND_HISTFILE__] => /Users/junweiluo/Library/Application Support/JetBrains/PhpStorm2020.3/terminal/history/history-822
    [TMPDIR] => /var/folders/81/hwj2hq1j4g11sr1yzwf32yfr0000gn/T/
    [SHELL] => /bin/zsh
    [LOGNAME] => junweiluo
    [XPC_SERVICE_NAME] => 0
    [LC_CTYPE] => zh_CN.UTF-8
    [XPC_FLAGS] => 0x0
    [__CFBundleIdentifier] => com.jetbrains.PhpStorm
    [TERM] => xterm-256color
    [TERMINAL_EMULATOR] => JetBrains-JediTerm
    [SHLVL] => 1
    [PWD] => /usr/local/var/www/risk-control-api
    [OLDPWD] => /usr/local/var/www/risk-control-api
    [CONDA_EXE] => /Users/junweiluo/opt/anaconda3/bin/conda
    [_CE_M] => 
    [_CE_CONDA] => 
    [CONDA_PYTHON_EXE] => /Users/junweiluo/opt/anaconda3/bin/python
    [CONDA_SHLVL] => 1
    [CONDA_PREFIX] => /Users/junweiluo/opt/anaconda3
    [CONDA_DEFAULT_ENV] => base
    [CONDA_PROMPT_MODIFIER] => (base) 
    [_] => /usr/local/opt/php@7.4/bin/php
    [PHP_SELF] => start.php
    [SCRIPT_NAME] => start.php
    [SCRIPT_FILENAME] => start.php
    [PATH_TRANSLATED] => start.php
    [DOCUMENT_ROOT] => 
    [REQUEST_TIME_FLOAT] => 1680081333.9622
    [REQUEST_TIME] => 1680081333
    [argv] => Array
        (
            [0] => start.php
            [1] => start
        )

    [argc] => 2
)
857 2 0
2个回答

PHP甩JAVA一条街

这个超全局变量在webman没用的

  • luojunwei 2023-03-29

    那怎么办,目前项目需要很多数据都在$_SERVER里面

北月

你可以使用中间件把需要用到的参数注入 $_SERVER 中,例子如下:

  1. 创建中间件文件:app/middleware/ServerArgsInject.php,内容如下:
<?php
namespace app\middleware;

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

class ServerArgsInject implements MiddlewareInterface
{
    public function process(Request $request, callable $handler) : Response
    {
        // 需要啥自己注入即可...
        $_SERVER['REQUEST_METHOD'] =$request->method();
        $_SERVER['REQUEST_URI'] = $request->uri();
        $_SERVER['QUERY_STRING'] = $request->queryString();

        $response = $handler($request);

        return $response;
    }
}
  1. 修改 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\ServerArgsInject::class,
    ]
];
  • 暂无评论
年代过于久远,无法发表回答
×
🔝