aura/web-project

由 Aura v2 包构建的最小化 Web 框架

安装次数: 1,572

依赖者: 0

建议者: 0

安全性: 0

星标: 62

关注者: 16

分支: 11

开放问题: 0

类型:项目

2.0.2 2015-03-28 21:49 UTC

This package is auto-updated.

Last update: 2024-09-11 04:15:24 UTC


README

本包为 Web 项目提供最小化框架。

“最小化”意味着非常小。本包仅提供依赖注入容器、配置系统、路由器、分发器、一对请求和响应对象以及日志实例。

这种最小化实现不应被视为“限制性”。DI 容器具有两阶段配置系统,允许广泛的程序化服务定义。路由器和分发器旨在迭代重构,因此您可以从类似微框架的闭包控制器开始,逐步过渡到更复杂的自定义控制器对象。

前言

要求

本项目需要 PHP 5.4 或更高版本;我们原则上推荐使用 PHP 的最新可用版本。

与 Aura 库包不同,本项目包具有用户空间依赖,这些依赖本身可能还有其他依赖

安装

通过 Composer 安装此项目到您选择的 {$PROJECT_PATH}

composer create-project aura/web-project {$PROJECT_PATH}

这将创建项目骨架并安装所有必要的包。

测试

Build Status

要在命令行运行单元测试,请在包根目录下执行 ./phpunit.sh。这需要 PHPUnit 可用作为 phpunit

或者,在安装项目后,使用 web/ 目录作为文档根启动内置 PHP 服务器

cd {$PROJECT_PATH}
php -S localhost:8000 -t web/

当您浏览到 http://localhost:8000 时,您应该看到输出“Hello World!”。之后终止内置服务器进程。(请务必仅将内置 PHP 服务器用于测试,绝不要用于生产。)

PSR 兼容性

本项目试图遵守 PSR-1PSR-2PSR-4。如果您发现合规性疏忽,请通过拉取请求发送补丁。

社区

要提问、提供反馈或与 Aura 社区进行其他交流,请加入我们的 Google Group,关注 @auraphp,或在 Freenode 的 #auraphp 上与我们聊天。

服务

本包使用以下服务定义

本项目重置以下服务

  • aura/project-kernel:logger:一个 Monolog\Logger 实例

入门指南

组件包

本项目将一组独立的 Aura 包组合成一个整体。每个包的操作都有单独的文档。

依赖注入 容器 是 Aura 项目操作的核心。请在继续之前熟悉 Aura.Di 文档

您还应该熟悉 Aura.RouterAura.Dispatcher 以及 Aura.Web请求响应 对象。

项目配置

每个 Aura 项目都使用相同的配置方式。有关更多信息,请参阅共享配置文档

日志记录

项目会自动将日志记录到 {$PROJECT_PATH}/tmp/log/{$mode}.log。如果您想更改特定配置模式的日志行为,请编辑相关的配置文件(例如,config/Dev.php)以修改 aura/project-kernel:logger 服务。

路由和分发

我们通过项目级别的 config/ 类文件配置路由和分发。如果某个路由需要在每个配置模式下都可用,请编辑项目级别的 config/Common.php 类文件。如果它只需要在特定模式下可用,例如 dev,则编辑该模式的配置文件。

以下是三种不同的路由和分发风格。

微框架风格

Aura 是第一个遵循 Action Domain Responder 模式的框架。下面是一个微框架风格的路由示例,其中动作逻辑嵌入在路由参数中。在 modifyWebRouter() 配置方法中,我们检索共享的 aura/web-kernel:requestaura/web-kernel:response 服务,以及 aura/web-kernel:router 服务。然后添加路由名称 blog.read 并将动作代码嵌入为闭包。

<?php
namespace Aura\Web_Project\_Config;

use Aura\Di\Config;
use Aura\Di\Container;

class Common extends Config
{
    // ...

    public function modifyWebRouter(Container $di)
    {
        $request = $di->get('aura/web-kernel:request');
        $response = $di->get('aura/web-kernel:response');

        $router = $di->get('aura/web-kernel:router');
        $router
            ->add('blog.read', '/blog/read/{id}')
            ->addValues(array(
                'action' => function ($id) use ($request, $response) {
                    $content = "Reading blog post $id";
                    $response->content->set(htmlspecialchars(
                        $content, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8'
                    ));
                }
            ));
    }

    // ...
}
?>

现在,您可以启动内置的 PHP 服务器以运行应用程序...

cd {$PROJECT_PATH}
php -S localhost:8000 -t web/

...然后浏览到 http://localhost:8000/blog/read/88 以查看应用程序输出。

修改后的微框架风格

我们可以修改上述示例,将控制器逻辑放在分发器中而不是路由本身。

将动作闭包提取到分发器下,命名为 blog.read。然后,在路由中使用与分发器中名称匹配的 action 值。

<?php
namespace Aura\Web_Project\_Config;

use Aura\Di\Config;
use Aura\Di\Container;

class Common extends Config
{
    // ...


    public function modifyWebRouter(Container $di)
    {
        $router = $di->get('aura/web-kernel:router');
        $router
            ->add('blog.read', '/blog/read/{id}')
            ->addValues(array(
                'action' => 'blog.read',
            ));
    }

    public function modifyWebDispatcher(Container $di)
    {
        $request = $di->get('aura/web-kernel:request');
        $response = $di->get('aura/web-kernel:response');

        $dispatcher = $di->get('aura/web-kernel:dispatcher');
        $dispatcher->setObject(
            'blog.read',
            function ($id) use ($request, $response) {
                $content = "Reading blog post $id";
                $response->content->set(htmlspecialchars(
                    $content, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8'
                ));
            }
        );

    }

    // ...
}
?>

现在,您可以启动内置的 PHP 服务器以运行应用程序...

cd {$PROJECT_PATH}
php -S localhost:8000 -t web/

...然后浏览到 http://localhost:8000/blog/read/88 以查看应用程序输出。

全栈风格

您可以从微风格迁移到全栈风格(或者一开始就使用全栈风格)。

首先,定义一个动作类并将其放置在项目的 src/ 目录中。

<?php
/**
 * {$PROJECT_PATH}/src/App/Actions/BlogReadAction.php
 */
namespace App\Actions;

use Aura\Web\Request;
use Aura\Web\Response;

class BlogReadAction
{
    public function __construct(Request $request, Response $response)
    {
        $this->request = $request;
        $this->response = $response;
    }

    public function __invoke($id)
    {
        $content = "Reading blog post $id";
        $this->response->content->set(htmlspecialchars(
            $content, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8'
        ));
    }
}
?>

接下来,通过 DI 容器 告知项目如何构建 BlogReadAction。编辑项目的 config/Common.php 文件以配置 Containeraura/web-kernel:requestaura/web-kernel:response 服务对象传递给 BlogReadAction 构造函数。

<?php
namespace Aura\Web_Project\_Config;

use Aura\Di\Config;
use Aura\Di\Container;

class Common extends Config
{
    public function define(Container $di)
    {
        $di->set('aura/project-kernel:logger', $di->lazyNew('Monolog\Logger'));

        $di->params['App\Actions\BlogReadAction'] = array(
            'request' => $di->lazyGet('aura/web-kernel:request'),
            'response' => $di->lazyGet('aura/web-kernel:response'),
        );
    }

    // ...
}
?>

之后,将 App\Actions\BlogReadAction 对象放置在分发器下,命名为 blog.read 作为懒加载实例...

<?php
namespace Aura\Web_Project\_Config;

use Aura\Di\Config;
use Aura\Di\Container;

class Common extends Config
{
    // ...

    public function modifyWebDispatcher(Container $di)
    {
        $dispatcher = $di->get('aura/web-kernel:dispatcher');
        $dispatcher->setObject(
            'blog.read',
            $di->lazyNew('App\Actions\BlogReadAction')
        );
    }

    // ...
}
?>

...最后,将路由指向 blog.read 动作对象

<?php
namespace Aura\Web_Project\_Config;

use Aura\Di\Config;
use Aura\Di\Container;

class Common extends Config
{
    // ...

    public function modifyWebRouter(Container $di)
    {
        $router = $di->get('aura/web-kernel:router');
        $router->add('blog.read', '/blog/read/{id}');
    }

    // ...
}
?>

现在,您可以启动内置的 PHP 服务器以运行应用程序...

cd {$PROJECT_PATH}
php -S localhost:8000 -t web/

...然后浏览到 http://localhost:8000/blog/read/88 以查看应用程序输出。

其他变体

这些只是路由和分发器交互的一些常见变体;还有许多其他组合