windwalker / application
Windwalker 应用程序包
3.5.25-beta2
2019-10-26 15:42 UTC
Requires
- php: >=7.1.3
- psr/log: ~1.0
- windwalker/environment: ~3.0
- windwalker/io: ~3.0
- windwalker/structure: ~3.0
- windwalker/uri: ~3.0
Requires (Dev)
- windwalker/http: ~3.0
- windwalker/test: ~3.0
- 3.x-dev
- dev-master / 3.x-dev
- 3.5.25-beta2
- 3.5.23
- 3.5.22
- 3.5.21
- 3.5.20
- 3.5.19
- 3.5.18
- 3.5.17
- 3.5.16
- 3.5.15
- 3.5.14
- 3.5.13
- 3.5.12
- 3.5.11
- 3.5.10
- 3.5.9
- 3.5.8
- 3.5.7
- 3.5.6
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5
- 3.4.9
- 3.4.8
- 3.4.7
- 3.4.6
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4
- 3.3.2
- 3.3.1
- 3.3
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1
- 3.0.1
- 3.0
- 3.0-beta2
- 3.0-beta
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.2
- 2.1.1
- 2.1
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-beta2
- 2.0.0-beta1
- 2.0.0-alpha
- dev-test
This package is auto-updated.
Last update: 2024-09-18 11:13:26 UTC
README
Windwalker 应用程序是作为系统主入口的核心。
通过 Composer 安装
将以下内容添加到您的 composer.json
文件的 require 块中。
{ "require": { "windwalker/application": "~3.0" } }
创建应用程序
创建应用程序并扩展 doExecute()
方法。
use Windwalker\Application\AbstractApplication; use Windwalker\IO\Input; use Windwalker\Structure\Structure; class MyApplication extends AbstractApplication { protected function init() { // Do stuff. // Get config $this->get('foo'); // bar } public function doExecute() { try { // Some code here... } catch (\Exception $e) { Error::renderErrorPage(); } return true; } } $app = new MyApplication(new Structure(array('foo' => 'bar'))); $app->execute();
配置是 Structure
对象,请参阅 Windwalker Structure
Web 应用程序
AbstractWebApplication
包含 WebEnvironment
和 WenHttpServer
对象,帮助我们处理 HTTP 请求和输出。
Web 环境
使用 WebEnvironment
获取浏览器或服务器信息。
$this->environment->browser->getBrowser(); // Get browser name
使用 Platform
获取服务器信息。
$this->environment->platform->isUnix();
请参阅:环境包
PSR7 处理器
dispatch()
是一个标准的 PSR7 处理器,因此我们可以在其中编写逻辑,只需返回 Response 对象和 Application 中的 WebHttpServer
对象,它将渲染给客户端。
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Windwalker\Application\AbstractWebApplication; class MyHttpKernel extends AbstractWebApplication { public function dispatch(Request $request, Response $response, $next = null) { // Get request query $query = $request->getQueryParams(); // Get Psr Uri $uri = $request->getUri(); // Write body $response->getBody()->write('<h1>Hello World~~~!</h1>'); return $response; } } $app = new MyHttpKernel; $app->execute();
结果
<h1>Hello World~~~!</h1>
错误处理器
将错误处理器设置为最终处理器,以便我们可以在 dispatch()
中使用它。
class MyHttpKernel extends AbstractWebApplication { public function dispatch(Request $request, Response $response, $next = null) { try { throw new \Exception('Whoops~', 500); } catch (\Exception $e) { return $next($e, $request, $response); } return $response; } } $app = new MyHttpKernel; $app->setFinalHandler(function (Exception $e, Request $request, Response $response) { $response->getBody()->write(sprintf('<h1>Error %s. Message: %s</h1>', $e->getCode(), $e->getMessage())); }); $app->execute();
结果
<h1>Error 500. Message: Whoops~</h1>
CLI 应用程序
这是一个简单的 CLI 应用程序示例。
// app.php use Windwalker\Application\AbstractCliApplication; class MyCliApp extends AbstractCliApplication { public function doExecute() { // Get options (-h) $help = $this->io->get('h'); if ($help) { $msg = <<<MSG Help message: version 1.0 ------------------------------------ myapp.php <command> [-options] foo Description of this command. bar Description of this command. help Description of this command. MSG; $this->io->out($msg); $this->close(); } // Get arguments $arg = $this->getArgument(0); // Do some stuff... return 0; // Exit code 0 means success } } $app = new MyCliApp; $app->execute();
现在我们可以通过 PHP CLI 访问此应用程序
php app.php arg1 arg2 -h --option --foo bar --n=a
请参阅:Windwalker IO