xp-framework/scriptlet

此软件包已被废弃且不再维护。没有建议的替代软件包。

XP Framework 的 Scriptlets

v9.2.2 2018-08-24 23:23 UTC

README

Build Status on TravisCI XP Framework Module BSD Licence Required PHP 5.6+ Supports PHP 7.0+ Latest Stable Version

在 XP Framework 中运行的任何网页都是脚本,最初都是这样的。每个高级 API 都是从简单的 HttpScriptlet 类派生出来的:RestScriptletWorkflowScriptlet 等...

HttpScriptlet 类

scriptlet.HttpScriptlet 类是任何所谓脚本的基础类。脚本是可以处理 HTTP 请求的东西。

在 XP Framework 中以最简单的方式响应 HTTP 请求如下

namespace com\example\web;

class HelloScriptlet extends \scriptlet\HttpScriptlet {

  /**
   * Perform GET request
   *
   * @param  scriptlet.Request $request
   * @param  scriptlet.Response $response
   * @throws scriptlet.ScriptletException
   */
  public function doGet($request, $response) {
    $response->write(sprintf('<!DOCTYPE html>
      <html>
        <head><title>Hello World scriptlet</title></head>
        <body>
          <h1>Hello %s</h1>
        </body>
      </html>',
      htmlspecialchars($request->getParam('name', 'World'))
    ));
  }
}

此代码生成一个显示标题 "Hello World" 或 "Hello $something" 的 HTML 页面,前提是提供了名为 "name" 的 GET 参数。

覆盖 doPost() 或其他以 HTTP 请求类型命名的任何方法,以提供这些请求类型。

运行它

使用 xp web 运行器来运行您的脚本

$ xp web com.example.web.HelloScriptlet
[xpws-dev#7312] running localhost:8080 @ /path/to/web/project - Press <Enter> to exit

现在在您的浏览器中打开 http://localhost:8080/