iamshobe/flight

轻量级 PHP 框架,用于调度和路由

安装: 12

依赖项: 0

建议者: 0

安全性: 0

星星: 0

关注者: 2

分支: 1

开放问题: 0

类型:框架

v0.2.2 2018-07-21 18:30 UTC

This package is not auto-updated.

Last update: 2024-09-29 06:25:38 UTC


README

此框架用于简单的网络导航。最初我想要一个类似于 Python Django 框架的东西,但更简单,我想体验一下网络框架的开发——只是一个有趣的副项目。

我遇到的第一个基本的调度器是 https://github.com/noodlehaus/dispatch/blob/master/dispatch.php,但我想找一个更复杂的(支持嵌套 URI 和正则表达式!),因此部分代码是从那个存储库中提取的,并用类包装了。

如何安装?

在你的项目中

$ composer require iamshobe/flight
$ composer update  # if already installed
require_once "./vendor/autoload.php";

文档

\Flight
class Dispatcher($base_dir, $urls, $use_static=true)

$base_dir - 应用程序根目录的目录。$urls - 使用 URL 类对象创建的根 URL 数组。$use_static - 如果应用程序使用静态文件夹,则为 true/false。

Dispatcher::dispatch(...$args) - 调度方法 - 在主文件末尾调用。

  • ...$args - 发送到所有视图的参数。
\Flight\URLS
class URL($regex, $handler, $methods = ["GET", "POST", "PUT", "DELETE", "PATCH"])

$regex - URI 的正则表达式。$handler - 将被调用的处理视图。$methods - URL 应该在哪些方法上活动。

class IncludeURLS($urls)

$urls - 应从不同位置包含的展开子 URL。

\Flight\Views
class View()

View::__invoke($vars) - 通过继承视图应该重写此方法。返回值应该是一个响应函数,一旦视图启动就会激活。

static View::response($data, $status_code = 200, $headers = []) - 响应函数。$data - 要发送的数据 $status_code - 响应状态码。$headers - 响应的头部。

View::static_file($path) - 提供静态文件。$path - 文件的路径。

View::page($path, $vars) - 渲染 phtml 文件。$path - 文件的路径。$vars - 应传递给模板文件的变量。

class StaticFile()

静态文件提供视图。

使用示例

.htaccess 文件

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule .* ./index.php

你的项目工作树

project_dir/
+-- vendor/
|   +-- .....(libs)....
|   +-- autoload.php
+-- src/
|   +-- URLS
|   |   +-- URLS.php
|   +-- Views
|   |   +-- Index.php
|   |   +-- File.php
+-- static/
|  +-- js/
|  |  +-- test.js
+-- template/
|  +-- index.phtml
+-- index.php

src/Views/Index.php

<?php namespace App\Views;
use \Flight\Views\View;


class Index extends View {
    public function __invoke($vars) {
        $json = json_encode(["hello" => $vars["hello"]]);
        return $this->response($json, 200, ["content-type" => "application/json"]);
    }
}

src/Views/File.php

<?php namespace App\Views;
use \Flight\Views\View;

class File extends View {
    public function __invoke($vars) {
        return $this->page("index.phtml");
    }
}

src/URLS/URLS.php

<?php namespace App\URLS;
use \Flight\URLS\URL;

class URLS {
    function __invoke()
    {
        return [
            new URL("^lol$", new \App\Views\File()),
            new URL("^(?P<hello>.*)$", new \App\Views\Index())
        ];
    }
}

index.php

<?php

require_once './lib/flight/loader.php';
require_once "./urls.php";


(new \Flight\Dispatcher(__DIR__, (new App\URLS\URLS())()))->dispatch();

templates/index.phtml

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="static/test.js"></script> <!-- This is an example for static file import -->
    This is the main file!
</body>
</html>

嵌套 URLS

URLS2.php

<?php namespace App\URLS;
use \Flight\URLS\URL;

class URLS2 {
    function __invoke()
    {
        return [
            // this will cause a prefix of http://example.com/lol....
            new URL("^lol$", new \Flight\URLS\IncludeURLS((new \App\URLS\URLS())())) 
        ];
    }
}

URLS.php

<?php namespace App\URLS;
use \Flight\URLS\URL;

class URLS {
    function __invoke()
    {
        return [
            // this will cause a url of http://example.com/lollol
            new URL("^lol$", new \App\Views\File(), ["GET"]),
            // this will cause a prefix of http://example.com/lol(.*)
            // where everything in brackets will be passed to hello var array.
            new URL("^(?P<hello>.*)$", new \App\Views\Index(). ["POST"])
        ];
    }
}