装甲/框架

Armor旨在成为PHP开发者有用的路由框架。

v1.3.0-alpha.0 2020-07-25 19:11 UTC

README

Latest Stable Version Build Status Armor Health Minimum PHP Version Support Latest Dev Version Packagist Downloads License

Armor (A Routing and MORe Things Framework)旨在成为PHP开发者有用的路由框架。

它实现了处理请求并响应它们的类和方法。除此之外,它甚至可以接收扩展,或者著名的"插件",无论你叫它什么。

以下,你可以了解如何安装它,如何使用它来处理请求,以及如何使用扩展库ArmorUI创建模板并将它们作为响应发送。

入门

安装Armor

这一步非常简单:只需使用Composer

composer require armor/framework

实现Armor

首先,当创建一个使用Armor的应用程序时,你必须创建一个应用程序实例

<?php
require "../vendor/autoload.php";

$app = new Armor\Application();

注意:你可以选择传递一个文本编码器作为参数,它将用于最初编码响应内容。默认情况下,它使用utf8_encode

然后在主文件的底部,放置对Application#run方法的调用

$app->run();

因此,文件应该是这样的

<?php
require "../vendor/autoload.php";

$app = new Armor\Application();

//The request handlers
/**
 * $app->get('/', function() {...});
 * 
 * $app->get('/path/to', function() {...});
 * 
 */

$app->run();

注意:你应该创建.htaccess文件,将所有请求重定向到主文件(index.php

现在,当我们谈到正确处理请求时,我们必须说Armor提供了一个简单易用的方式来处理请求,基于执行请求所使用的方法

// Handles a GET request for "/path/to"
$app->get('/path/to', function() {
    //...
});
// Handles a POST request for "/path/to"
$app->post('/path/to', function() {
    //...
});

目前,Armor只处理GET和POST请求。但是,将来可能会支持更多。

作为参数传递的回调函数必须至少接收两个参数:一个Request对象和一个Response对象。

Request对象提供了有关请求路径、查询参数和路由参数的信息。最后一个名称可能听起来不熟悉,但如果你是后端开发者,你可能已经见过类似的东西

$app->get('/path/to/$(section)', function(Request $req, Response $res) {
    if ($req->path['section'] == 'something') {
        //do something
    }
    //...
});

Response对象提供了很多方法来向响应中添加内容。

以下是一个处理请求和发送响应的小例子

$app = new Armor\Application();

$app->get('/', function(Request $req, Response $res) {
    $res->append("<html>
        <body>
            <h1>Hello, World!</h1>
            <p>This is an example page, and you are accessing {$req->path->absolute}</p>
        </body>
    </html>");

    return $res->end();
});

$app->run();

正如你所看到的,我们正在处理对/路径的请求。我们向响应中添加了一条简单的消息,它使用了Request对象的path属性的absolute值。最后,我们完成响应,并返回最终结果。这个"返回"被Armor用来知道响应是否已正确构建,或者是否发生了意外。如果是的话,Armor会抛出ResponseNotCorrectlyCompletedException

关于Armor还有很多要了解的。将来,它可能会被更详细的文档完全覆盖。

关于使用模板的简短讨论

有一个可以用来创建一些UI的扩展库,称为ArmorUI。目前,它提供了两个类:TemplateManager,它负责从其目录中加载模板,以及Template,它是模板对象本身。它的工作方式如下

/** 
 * This example is a snippet taken and adapted from the 'example01', available at https://github.com/14mPr0gr4mm3r/armor-examples
 * @author 14mPr0gr4mm3r
 * @license MIT
*/

$templ = new ArmorUI\TemplateManager("./", ['header', 'index']);

$app->get('/', function(Request $req, Response $res) use($templ) {
    $templ->getTemplate('index')->sendAsResponse($res);

    return $res->end();
});

在此,我们加载了两个模板:“header.templ.armor”和“index.templ.armor”。它们与源文件位于同一目录下。我们从闭包内部加载管理器,并使用它来加载index模板(getTemplate)并发送它。发送模板时,我们将Response对象通过引用传递给Template#sendAsResponse方法。然后,我们完成请求处理过程。

相反,为了避免在“请求处理闭包”中添加use关键字,我们可以使用Application#use方法,如下所示

$templ = new ArmorUI\TemplateManager("./", ['header', 'index']);

$app->use('templ', $templ);

$app->get('/', function(Request $req, Response $res, Application $app) {
    $app['templ']->getTemplate('index')->sendAsResponse($res);

    return $res->end();
});

在这里,我们看到第三个,但可选的参数:用户创建并用于定义路由的Application实例。

最终考虑

我承认这只是一小部分代码。但是,在未来,文档的质量和细节可能会得到改善。

来自14mPr0gr4mm3r的良苦用心,祝您学习愉快。