vtardia/simphle

该包已弃用,不再维护。未建议替代包。

快速简单的PHP开发服务器

1.0.3 2016-02-11 17:29 UTC

This package is auto-updated.

Last update: 2022-01-23 21:10:11 UTC


README

Simphle是一个友好的PHP内置Web服务器的包装器,具有一些易于开发的便捷功能。

要求

安装

  1. Composer
$ composer [global] require vtardia/simphle

使用 global 选项在系统上全局安装

  1. 自定义

将此存储库克隆到共享目录中,例如Mac上的 /usr/local/php/simphle/Users/Shared/php/Simphle,然后运行 composer install

bin 目录添加到本地路径或创建指向 bin/simphle 可执行文件的链接

   $ ln -s /usr/local/php/simphle/bin/simphle ~/bin/simphle

用法

基本

在当前目录中以默认设置启动服务器

$ cd /path/to/myapp/docroot
$ simphle

中级

从命令行以自定义设置启动服务器

$ cd /path/to/myapp
$ simphle [-H host] [-p port] [-c path/to/php.ini] [-r path/to/router.php] [path/to/docroot]

高级

在项目目录中创建一个包含自定义设置的 server.json 文件,然后从该路径启动 simphle

{
    "host":"0.0.0.0",
    "port":5000,
    "docroot":"public",
    "router":"myrouter.php|default",
    "ini":"mysettings.ini|default",
    "controller": "mycontroller.php",
    "env": {
        "myvar": "somevalue",
        "anothervar": {
            "one": "foo",
            "two": "bar"
        }
    }
}

routerini 文件路径相对于当前工作目录。主要的 Simphle 脚本首先在当前工作目录中搜索这些文件,然后搜索 Simphle 的 share 目录。预设路由器和 INI 文件不需要 .php.ini 扩展名。

controller 文件路径相对于文档根目录。

使用 Composer 启动

composer.json 中创建一个 scripts 部分

"scripts": {
    "server": "simphle [options]"
}

然后使用以下命令启动

$ composer server

默认路由器

Simphle 的默认路由器试图模拟典型的 Apache .htaccess 文件

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

如果请求的 URI 存在,则尝试获取它,如果不存在,则搜索 index.php,最后回退到“404未找到”错误。

使用前端控制器

通过指定 controller 设置,Simphle 使用此文件作为重写目标,而不是 index.php,因此您可以使用例如 app.php

自定义环境

env 部分定义了通过 $_ENV 超全局变量传递给 PHP 文件的环境变量。JSON 对象被转换为关联数组。

有时 $_ENV 超全局变量不为空。这取决于 php.ini 中的 variables_order 设置

; This directive determines which super global arrays are registered when PHP
; starts up. If the register_globals directive is enabled, it also determines
; what order variables are populated into the global space. G,P,C,E & S are
; abbreviations for the following respective super globals: GET, POST, COOKIE,
; ENV and SERVER. There is a performance penalty paid for the registration of
; these arrays and because ENV is not as commonly used as the others, ENV is
; is not recommended on productions servers. You can still get access to
; the environment variables through getenv() should you need to.
; Default Value: "EGPCS"
; Development Value: "GPCS"
; Production Value: "GPCS";
; https://php.ac.cn/variables-order
variables_order = "GPCS"

此设置可以被本地 htaccessphp.ini 文件覆盖。

许可证

Simphle 采用 MIT 许可证授权 - 详细信息请参阅 LICENSE 文件。