aeberdinelli/express-php

此包已被废弃,不再维护。未建议替代包。
关于此包的最新版本(v1.1.0)没有可用的许可证信息。

ExpressJS 框架的 PHP 版本克隆

v1.1.0 2018-04-12 23:48 UTC

This package is not auto-updated.

Last update: 2021-08-20 12:18:51 UTC


README

此框架试图模仿 NodeJS ExpressJS 框架 的编写风格。它没有事件循环,只是模仿了 ExpressJS 提供的方法和助手名称。

安装

注意:为了运行 ExpressPHP,您需要 PHP >= 7.0 和 Apache。

首选的安装方法是使用 Composer

composer require aeberdinelli/express-php v1.1.0

然后,将 .htaccess 文件移动到您站点的根目录,安装完成

mv vendor/aeberdinelli/express-php/.htaccess ./.htaccess

使用

如果您使用 Composer 进行安装,可以直接这样做

<?php
include __DIR__.'/vendor/autoload.php';

use Express\Express;
use Express\Router;

$express = new Express();
$router = new Router();

$router->get('/', function($req, $res) {
	$res->send('hello world!');
});

$express->listen($router);
?>

路由

路由是通过 Router 实例处理的,例如

$router = new Router();
$router->get('/', function($req, $res) {
    // This will be called when someone goes to the main page using GET method.
});

您也可以使用 post() 替代 get() 来处理 POST 请求。对于 put() 和 delete() 也同样如此。

带有动态参数的路由

您可以使用参数来路由动态 URL,例如

$router = new Router();
$router->get('/:something/:else', function($req, $res) {
    /**
     * Now let's imagine someone enters to URL: /hello/bye, then:
     *
     * $req->params->something will contain 'hello'
     * $req->params->else will contain 'bye'
     */
});

响应

如果您正在开发 API,例如,您可以简单地发送 json

$router->post('/', function($req, $res) {
	$res->json(array(
		'error'		=> false,
		'message'	=> 'Hello'
	));
});

您还可以使用以下方式发送自定义 HTTP 响应代码

$router->post('/', function($req, $res) {
	$res->status(201)->json({
		'error'		=> false,
		'message'	=> 'Created!'
	});
});

提示:此存储库中的 index.php 文件中有更多示例。

静态文件

如果您希望提供静态文件(如图片、仅 HTML),可以使用

// If you visit /static/image.png, this will return the file views/public/image.png
$router->use('/static', $express->static('views/public'));

模板引擎

您可以使用 Pug(前 Jade)和 Mustache。以下是一个示例

// Configure the engine to Pug
$express->set('view engine','pug');

// Jade was renamed to Pug, but we recognize it ;)
$express->set('view engine','jade');

// Or Mustache
$express->set('view engine','mustache');

// Set the path to the template files
$express->set('views','./views/pug');

// Now you can do something like this
$router->get('/', function($req, $res) {
	$res->render('index.jade');
});

// Or this
$router->get('/users/:username', function($req, $res) {
	$res->render('index.jade', array(
		'name'	=> $req->params->username
	));

	// Now in jade, you can use #{name} to get that variable!
});

CSS 预编译器

如果您想使用 Less 而不是 CSS,可以这样使用。以下是一个示例

use \Express\ExpressLess;

/**
 * Let's say you have a /less folder on your project
 * And you want every request that goes into /css to load the less file within that folder instead
 *
 * In this example /css/global.css will load the compiled version of /less/global.less
 * Same for /css/something.css -> /less/something.less
 */

$less = new ExpressLess($express, array(
	'source'	=> __DIR__.'/less',
	'dest'		=> '/css'
));

// Yes, it's that simple.

请求信息

  • 无论您是处理 POST 还是 PUT,您都可以在 $res->body 中找到请求正文。
  • 查询字符串位于 $req->query 下
  • cookie 位于 $req->cookies 中
  • 所有请求头都在 $req->headers 中