aura/web-project 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aura/web-project

Composer 安装命令:

composer create-project aura/web-project

包简介

A minimal web framework built from Aura v2 packages

关键字:

README 文档

README

This package provides a minimal framework for web projects.

By "minimal" we mean very minimal. The package provides only a dependency injection container, a configuration system, a router, a dispatcher, a pair of request and response objects, and a logging instance.

This minimal implementation should not be taken as "restrictive". The DI container, with its two-stage configuration system, allows a wide range of programmatic service definitions. The router and dispatcher are built with iterative refactoring in mind, so you can start with micro-framework-like closure controllers, and work your way into more complex controller objects of your own design.

Foreword

Requirements

This project requires PHP 5.4 or later; we recommend using the latest available version of PHP as a matter of principle.

Unlike Aura library packages, this project package has userland dependencies, which themselves may have other dependencies:

Installation

Install this project via Composer to a {$PROJECT_PATH} of your choosing:

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

This will create the project skeleton and install all of the necessary packages.

Tests

Build Status

To run the unit tests at the command line, issue ./phpunit.sh at the package root. This requires PHPUnit to be available as phpunit.

Alternatively, after you have installed the project, start the built-in PHP server with the web/ directory as the document root:

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

When you browse to http://localhost:8000 you should see "Hello World!" as the output. Terminate the built-in server process thereafter. (Be sure to use the built-in PHP server only for testing, never for production.)

PSR Compliance

This projects attempts to comply with PSR-1, PSR-2, and PSR-4. If you notice compliance oversights, please send a patch via pull request.

Community

To ask questions, provide feedback, or otherwise communicate with the Aura community, please join our Google Group, follow @auraphp on Twitter, or chat with us on #auraphp on Freenode.

Services

This package uses services defined by:

This project resets the following services:

  • aura/project-kernel:logger: an instance of Monolog\Logger

Getting Started

Component Packages

This project combines a collection of independent Aura packages into a cohesive whole. The operation of each package is documented separately.

The dependency injection Container is absolutely central to the operation of an Aura project. Please be familiar with the Aura.Di docs before continuing.

You should also familiarize yourself with Aura.Router, Aura.Dispatcher, and the Aura.Web Request and Response objects.

Project Configuration

Every Aura project is configured the same way. Please see the shared configuration docs for more information.

Logging

The project automatically logs to {$PROJECT_PATH}/tmp/log/{$mode}.log. If you want to change the logging behaviors for a particular config mode, edit the related config file (e.g., config/Dev.php) file to modify the aura/project-kernel:logger service.

Routing and Dispatching

We configure routing and dispatching via the project-level config/ class files. If a route needs to be available in every config mode, edit the project-level config/Common.php class file. If it only needs to be available in a specific mode, e.g. dev, then edit the config file for that mode.

Here are three different styles of routing and dispatching.

Micro-Framework Style

Aura is the first framework which follows the Action Domain Responder pattern. The following is an example of a micro-framework style route, where the action logic is embedded in the route params. In the modifyWebRouter() config method, we retrieve the shared aura/web-kernel:request and aura/web-kernel:response services, along with the aura/web-kernel:router service. We then add a route names blog.read and embed the action code as a closure.

<?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'
                    ));
                }
            ));
    }

    // ...
}
?>

You can now start up the built-in PHP server to get the application running ...

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

... and browse to http://localhost:8000/blog/read/88 to see the application output.

Modified Micro-Framework Style

We can modify the above example to put the controller logic in the dispatcher instead of the route itself.

Extract the action closure to the dispatcher under the name blog.read. Then, in the route, use a action value that matches the name in the dispatcher.

<?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'
                ));
            }
        );

    }

    // ...
}
?>

You can now start up the built-in PHP server to get the application running ...

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

... and browse to http://localhost:8000/blog/read/88 to see the application output.

Full-Stack Style

You can migrate from a micro style to a full-stack style (or start with full-stack style in the first place).

First, define an action class and place it in the project src/ directory.

<?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'
        ));
    }
}
?>

Next, tell the project how to build the BlogReadAction through the DI Container. Edit the project config/Common.php file to configure the Container to pass the aura/web-kernel:request and aura/web-kernel:response service objects to the BlogReadAction constructor.

<?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'),
        );
    }

    // ...
}
?>

After that, put the App\Actions\BlogReadAction object in the dispatcher under the name blog.read as a lazy-loaded instantiation ...

<?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')
        );
    }

    // ...
}
?>

... and finally, point the router to the blog.read action object:

<?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}');
    }

    // ...
}
?>

You can now start up the built-in PHP server to get the application running ...

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

... then browse to http://localhost:8000/blog/read/88 to see the application output.

Other Variations

These are only some common variations of router and dispatcher interactions; there are many other combinations.

aura/web-project 适用场景与选型建议

aura/web-project 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.59k 次下载、GitHub Stars 达 60, 最近一次更新时间为 2013 年 11 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「web」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 aura/web-project 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aura/web-project 我们能提供哪些服务?
定制开发 / 二次开发

基于 aura/web-project 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.59k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 60
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 60
  • Watchers: 14
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2013-11-21