v0.0.1 2024-03-08 12:46 UTC

This package is auto-updated.

Last update: 2024-09-08 13:59:25 UTC


README

Zenith是一个先进的PHP应用程序框架,通过结合Slim FrameworkCompass Router,为组件驱动开发提供灵感,实现了由NEXT.js路由启发的开发体验。

关键特性

  • 组件驱动开发:利用高度模块化的架构,允许使用可重用组件。
  • NEXT.js启发的路由:提供了一种直观的PHP路由方法,增强了应用程序的导航结构。
  • 基于Slim Framework:利用Slim Framework,为构建应用程序提供强大而稳健的基础。
  • 高级渲染技术:支持响应式和懒加载页面渲染,以优化用户体验。

快速开始

通过几个简单步骤创建和运行您的下一个PHP应用程序

  1. 项目设置

    使用Composer初始化新项目

    composer create-project robertkleinschuster/zenith-starter my-new-project
  2. 开发服务器

    启动开发服务器

    composer dev

    访问您的应用程序:https://:8080/

  3. 添加路由

    轻松地向应用程序引入新路由

    composer add-route /about-me

    示例:通过https://:8080/about-me访问route/about-me/page.php

目录结构

了解您项目的架构

components/       # Put functional components here
public/           # Webserver document root
routes/           # Route entrypoints
src/              # Object-oriented code for business logic
tests/            # Unit tests

使用示例

此示例演示了设置用户个人资料页面,重点关注自动路由注册、动态布局以及根据框架约定处理表单提交。

约定概述

  • 用于GET请求的page.php文件和用于布局的layout.php文件遵循基于它们所服务的路由的特定命名和目录结构约定。
  • 用于POST请求的action.php文件与page.phplayout.php文件一起放置,并遵循相同的路径相关约定。

步骤1:个人资料页面和布局

对于/user/{userId}路由,在routes目录内安排以下结构

  • routes/user/{userId}/内添加page.php以渲染个人资料内容

    <?php
    
    use Mosaic\Fragment;
    use Psr\Http\Message\ServerRequestInterface;
    
    return function(ServerRequestInterface $request, array $params) {
        $userId = $params['userId'];
        $content = "<h1>User Profile for User ID: {$userId}</h1>";
    
        return new Fragment($content);
    };
  • 在同一目录(routes/user/{userId}/)中创建layout.php以建立页面布局

    <?php
    
    use Mosaic\Fragment;
    use Mosaic\Renderer;
    
    return function(Renderer $renderer, $children) {
        $html = "<html><body>{$renderer->render($children)}</body></html>";
    
        return new Fragment($html);
    };

步骤2:表单提交操作

  • routes/user/{userId}/内实现action.php以处理表单提交
    <?php
    
    use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;
    
    return function(ServerRequestInterface $request, ResponseInterface $response) {
        $formData = $request->getParsedBody();
        $response->getBody()->write("Form data: " + htmlspecialchars($formData['data']));
    
        return $response;
    };