oscarotero/server

一个简单的类,用于模拟内置PHP web服务器中Apache的'mod_rewrite'功能

v0.2.0 2017-05-06 14:57 UTC

This package is auto-updated.

Last update: 2024-09-05 18:26:05 UTC


README

一个简单的类,用于从内置PHP web服务器模拟Apache的"mod_rewrite"功能。这提供了一个方便的方式来测试任何web应用,而不需要安装真正的web服务器软件。它适用于任何cms或框架。

安装

该软件包可以通过Composer安装和自动加载,名称为oscarotero/server

$ composer require oscarotero/server

用法

创建一个server.php文件,该文件将作为我们的服务器脚本。

//The file exists and can be served as is
if (Server::run()) {
    return false;
}

//Otherwise, go with normal php operations
require __DIR__.'/index.php';

启动PHP服务器

php -S localhost:8000 server.php

这就完成了,你可以在浏览器中的http://localhost:8000看到网站。

更改公共目录

默认情况下,使用getcwd获取服务器的基目录。但可以通过第一个参数来更改它。例如

//The file exists in public and can be served as is
if (Server::run(__DIR__.'/public')) {
    return false;
}

//Or include the index.php script
require_once __DIR__.'/public/index.php';

执行PHP文件

如果文件有PHP扩展名,则返回路径,因此可以包含它。仅在基本目录与当前目录不同且站点没有唯一的PHP文件(例如WordPress站点)时需要此功能。

if ($file = Server::run(__DIR__.'/wordpress')) {
    require $file;
}

API

如前例所示,方法Server::run()可以返回三个值

  • true 表示文件存在且可以直接由PHP服务器提供服务
  • string 表示文件存在但必须包含(是一个不能直接提供的PHP文件)
  • false 表示文件不存在