php express
Version 2.0.0
PHP Micro-FrameWork受Express.js启发
| 建造 | github页面 | 稳定的 | 执照 |
|---|---|---|---|
ext-json )如果您的系统尚未安装作曲家,则可以继续使用此命令行安装它:
$ curl -sS https://getcomposer.org/installer | php
接下来,将以下要求输入到项目根部中的composer.json文件。
{
"require" : {
"riverside/php-express" : " ^2.0 "
}
}最后,使用作曲家安装PHP-Express及其依赖性:
$ php composer.phar install
<?php
$ app = new Riverside Express Application ();
$ app -> get ( ' / ' , function ( $ req , $ res ) {
$ res -> send ( ' hello world ' );
}); <?php
// GET method route
$ app -> get ( ' / ' , function ( $ req , $ res ) {
$ res -> send ( ' GET request to the homepage ' );
});
// POST method route
$ app -> post ( ' / ' , function ( $ req , $ res ) {
$ res -> send ( ' POST request to the homepage ' );
}); <?php
$ app -> get ( ' / ' , function ( $ req , $ res ) {
$ res -> send ( ' root ' );
});
$ app -> get ( ' about ' , function ( $ req , $ res ) {
$ res -> send ( ' about ' );
});
$ app -> get ( ' random.text ' , function ( $ req , $ res ) {
$ res -> send ( ' random.text ' );
});| 方法 | 描述 |
|---|---|
| $ res-> end() | 结束响应过程。 |
| $ res-> json() | 发送JSON回复。 |
| $ res-> redirect() | 重定向请求。 |
| $ res-> render() | 渲染视图模板。 |
| $ res-> send() | 发送各种类型的响应。 |
| $ res-> sendstatus() | 设置响应状态代码并将其字符串表示形式发送为响应主体。 |
<?php
$ app -> route ( ' /book ' )
-> get ( function ( $ req , $ res ) {
$ res -> send ( ' Get a random book ' );
})
-> post ( function ( $ req , $ res ) {
$ res -> send ( ' Add a book ' );
})
-> put ( function ( $ req , $ res ) {
$ res -> send ( ' Update the book ' );
}); <?php
$ router = new Riverside Express Router ( $ app );
$ router -> param ( ' uuid ' , ' [a-fd]{8}-[a-fd]{4}-[a-fd]{4}-[a-fd]{4}-[a-fd]{12} ' );
$ router -> get ( ' / ' , function ( $ req , $ res ) {
$ res -> send ( ' Birds home page ' );
});
$ router -> get ( ' about ' , function ( $ req , $ res ) {
$ res -> send ( ' About birds ' );
});
$ router -> get ( ' ticket/:uuid/ ' , function ( $ req , $ res ) {
echo $ req -> params [ ' uuid ' ];
});
$ router -> run (); $ app -> use ( function ( $ req , $ res ) {
$ res -> header ( ' X-Frame-Options ' , ' DENY ' );
$ res -> header ( ' X-Powered-By ' , false );
});
$ app -> use ( ' /cors ' , function ( $ req , $ res ) {
$ res -> header ( ' Access-Control-Allow-Origin ' , ' * ' );
});