bchubbweb/phntm

一个侧重于开发体验和简单性的服务器端栈

v0.1.0 2024-04-04 10:31 UTC

This package is auto-updated.

Last update: 2024-09-14 12:17:29 UTC


README

一个轻量级的框架,设计得像魔法一样;处理现代Web应用程序中的枯燥部分,就像有人替你做一样。

安装

要将包安装到您的项目中,请使用以下命令

composer require bchubbweb/phntm

设置

在项目根目录下创建一个 .htaccess 文件,内容如下

RewriteEngine On
RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.webp|\.gif|\.jpeg|\.zip|\.css|\.svg|\.js|\.pdf|\.ttf)$
RewriteRule (.*) index.php [QSA,L]

显然,您必须在项目根目录下添加 index.php,以开始,请添加以下内容

<?php

namespace bchubbweb\PhntmImplementation;

require('vendor/autoload.php');

use bchubbweb\phntm\Phntm;

// remove 'profile: true' to disable profiling
Phntm::init(profile: true);

这将设置命名空间路由器,并确定对当前请求的处理方式。

接下来,将 Pages 命名空间添加到您的 composer.json 中,指向您希望使用的目录,尽管我建议在项目根目录下使用 pages/。

{
    "autoload": {
        "psr-4": {
            "Pages\\": "pages/"
        }
    }
}

然后启动您的本地服务器并按需操作

php -S localhost:3000

页面

您页面目录中的页面应扩展 bchubbweb\phntm\Resource\Page 类,并必须命名为 Page.php,这将让路由器知道这个命名空间可以被路由到。

<?php

namespace Pages\Home;

use bchubbweb\phntm\Resources\Page as PageTemplate;

class Page extends PageTemplate
{
    public function __construct() {
        parent::__construct();

        $this->setContent(<<<HTML
            <h1>home</h1>
        HTML);
    }
}

布局

类似于 NextJS,phntm 使用布局来包裹页面内容,这对于页眉和页脚、共享资产等内容非常有用。

<?php

namespace Pages;
 
use bchubbweb\phntm\Resources\Layout as LayoutTemplate;

class Layout extends LayoutTemplate
{
    public function __construct() {

        $this->registerAsset('/assets/layout.css');
        $this->registerAsset('/assets/layout.js');

        $this->setContent(<<<HTML
        <html>
            <head>
                <title>Phntm</title>
                <!-- head /-->
            </head>
            <body>
                <header>
                    <nav>
                        <ul>
                            <li><a href="/">Home</a></li>
                            <li><a href="/about">About</a></li>
                            <li><a href="/users">Users</a></li>
                        </ul>
                    </nav>
                </header>
                <main>
                    <!-- content /-->
                </main>
                <footer>
                    <p>Phntm</p>
                </footer>
                <!-- profiler /-->
            </body>
        </html>
        HTML);
    }
}

注释用于将页面内容注入布局、注册的资产以及启用时的情况分析脚本。

与 NextJS 不同,布局限制为单层,因此给定页面的命名空间中最接近的布局将被使用。

情况分析

要使用 phntm 情况分析器,请将 true 传递给 Phntm::init() 函数,这将分析请求并将情况分析脚本注入布局。

然后脚本从 /api/profiler 端点获取缓存的配置数据,并在表格中显示。