seekquarry / atto
单文件,纯PHP网络服务器和网络路由引擎
Requires
- php: >=5.5.0
- ext-pcre: *
- ext-spl: *
- ext-zip: *
- lib-pcre: *
README
Atto是一组单文件、低依赖、纯PHP服务器和路由引擎。目前,Atto包含两个服务器类:WebSite,一个纯PHP网络服务器和网络路由引擎,以及GopherSite,一个纯PHPGopher服务器和Gopher路由引擎。
-
Atto服务器可用于路由请求,因此可作为在Apache、nginx或lighttpd等传统服务器下使用的微型框架。
-
Atto可以作为具有其路由功能的应用程序的自立服务器使用。
-
Atto是请求事件驱动的,支持异步I/O进行网络流量。
-
与类似的PHP软件不同,作为Web或Gopher服务器,它实例化了传统的PHP超全局变量如$_GET、$_POST、$_REQUEST、$_COOKIE、$_SESSION、$_FILES等,并努力使快速编写PHP应用程序变得容易。
作为独立服务器
- Atto支持后台事件计时器。
- Atto在RAM中处理会话。
- Atto具有文件I/O方法fileGetContents和filePutContents,使用Marker算法缓存文件。
用法
<?php require 'atto_server_path/src/Website.php'; //this line needs to be changed use seekquarry\atto\Website; $test = new WebSite(); /* A Simple Atto WebSite used to display a Hello World landing page. After changing the require line above, you can run the example by typing: php index.php and pointing a browser to https://:8080/ */ $test->get('/', function() { ?> <!DOCTYPE html> <html> <head><title>Hello World - Atto Server</title></head> <body> <h1>Hello World!</h1> <div>My first atto server route!</div> </body> </html> <?php }); if($test->isCli()) { /* This line is used if the app is run from the command line with a line like: php index.php It causes the server to run on port 8080 */ $test->listen(8080); } else { /* This line is for when site is run under a web server like Apache, nginx, lighttpd, etc. The enclosing folder should contain an .htaccess file to redirect traffic through this index.php file. So redirects need to be on to use this example under a Apache, etc. */ $test->process(); }
安装
Atto服务器已在PHP 5.5、PHP 7和HHVM上进行了测试。目前HHVM下不支持HTTPS。
要安装软件,应已安装PHP。然后可以通过git clone
项目或从GitHub下载ZIP文件。
要在项目中使用Atto服务器,请在项目文件顶部添加以下行
require 'atto_server_path/src/Website.php'; //this line needs to be changed use seekquarry\atto\Website;
(如果需要Gopher服务器,则应使用GopherSite)如果您没有使用use
行,则要引用Website类,则需要添加整个命名空间路径。例如,
$test = new seekquarry\atto\Website();
如果您使用composer,则可以使用以下命令安装Atto服务器
composer require seekquarry/atto
然后执行composer install
或composer update
。要求"vendor/autoload.php"
应该足够允许Atto服务器根据您的代码需要自动加载。
更多示例
此项目的示例文件夹包含一系列示例,展示了Atto服务器的主要功能。
您可以通过在项目根文件夹中使用index.php脚本来测试特定的示例。例如,
php index.php 01
将运行第一个示例。