首页 > 后端开发 > php教程 > Zend表达模块的快速发展

Zend表达模块的快速发展

Jennifer Aniston
发布: 2025-02-09 08:35:12
原创
646 人浏览过

Rapid Development of Zend Expressive Modules

Zend Expressive模块快速开发指南:构建只读博客模块

本文将分享一些Zend Expressive模块开发技巧,帮助您快速构建一个功能完善的只读博客模块。请确保您已按照之前的教程设置好开发环境,包括安装和配置Zend Expressive、Doctrine、Gulp以及抽象反射工厂(约需10分钟)。

在本教程中,我们将快速构建一个简单的只读博客模块(从数据库中列出博客文章),展示Zend Expressive的快速开发能力。

模块设置

在您的Expressive应用中运行以下命令:

./vendor/bin/expressive module:create Blog
登录后复制

这将生成Blog模块的基本代码,并自动将其注册到您的应用程序和Composer自动加载器中。

Doctrine实体和数据库表

接下来,创建Blog实体和数据库表。首先,我们需要让应用程序知道该模块提供了Doctrine实体。

打开src/Blog/src/ConfigProvider.php,添加以下代码:

public function __invoke()
{
    return [
        'dependencies' => $this->getDependencies(),
        'doctrine'     => $this->getDoctrine(),
        'templates'    => $this->getTemplates(),
    ];
}

public function getDoctrine(): array
{
    return [
        'driver' => [
            'orm_default' => [
                'drivers' => [
                    'Blog\Entity' => 'blog_entity',
                ],
            ],
            'blog_entity' => [
                'class' => \Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver::class,
                'cache' => 'array',
                'paths' => [
                    dirname(__DIR__) . '/config/doctrine' => 'Blog\Entity',
                ],
            ],
        ],
    ];
}
登录后复制

src/Blog/config/doctrine目录下创建BlogPost.orm.yml文件,内容如下:

---
Blog\Entity\BlogPost:
  type: entity
  table: blog_post
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    title:
      type: string
      length: 255
    content:
      type: string
      length: 16777215
登录后复制

运行./vendor/bin/doctrine orm:generate-entities src。 由于Doctrine不支持PSR-4标准的目录结构,需要将src/Blog/Entity移动到src/Blog/src/Entity。 然后运行以下命令创建数据库表:

./vendor/bin/doctrine orm:schema-tool:create
登录后复制

您可以运行以下SQL语句填充数据库表:

INSERT INTO expressive.blog_post VALUES 
(null, 'Post 1', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'),
(null, 'Post 2', 'Mauris in libero laoreet, euismod lorem eget, tincidunt justo.'),
(null, 'Post 3', 'Donec sed diam congue, ultrices tellus at, venenatis felis.');
登录后复制

路由设置

Expressive模块不直接注册路由。我们需要使用一个小技巧来实现:创建src/Blog/src/Factory/RoutesDelegator.php文件,内容如下:

<?php
namespace Blog\Factory;

use Blog\Action;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Application;

class RoutesDelegator
{
    public function __invoke(ContainerInterface $container, $serviceName, callable $callback)
    {
        $app = $callback();
        include __DIR__ . '/../../config/routes.php';
        return $app;
    }
}
登录后复制

src/Blog/src/ConfigProvider.phpgetDependencies()方法中添加以下代码:

'delegators' => [
    \Zend\Expressive\Application::class => [
        Factory\RoutesDelegator::class,
    ],
],
登录后复制

创建src/Blog/config/routes.php文件,添加博客路由:

<?php

use Blog\Action;

$app->get('/blog', Action\BlogPostListAction::class, 'blog_post_list');
$app->get('/blog/view/:blog_post_id', Action\BlogPostViewAction::class, 'blog_post_view');
登录后复制

Action和模板

接下来,创建Action来处理路由请求,并创建模板文件。 (Action和模板代码与原文相同,此处省略,请参考原文。)

Rapid Development of Zend Expressive Modules

创建、编辑和删除功能的实现留作练习。

总结

本教程展示了使用Zend Expressive快速构建只读博客模块的简易性。只需少量文件和几分钟的工作,即可创建一个从数据库显示文章的列表页,并为后续添加/edit/delete等路由做好准备。

(以下为原文FAQs部分,略作调整)

Zend Expressive快速开发常见问题

  • 什么是Zend Expressive? Zend Expressive是一个基于PHP的微型中间件框架,构建于Zend Stratigility之上,支持PSR-7中间件。

  • 如何安装Zend Expressive? 使用Composer:composer require zendframework/zend-expressive

  • Zend Expressive的优势? 快速开发、简单灵活,支持各种应用类型(微服务到单体应用),支持多种路由和模板系统。

  • 如何在Zend Expressive中创建模块? 在应用的src目录下创建新目录,包含ConfigProvider类,返回模块配置数组(包含依赖项、路由和模板)。

  • 如何在Zend Expressive中添加路由? 在模块ConfigProvider类的配置数组的routes键中添加新条目。

  • 如何在Zend Expressive中使用模板? Zend Expressive支持多种模板引擎(Twig、Plates、Zend View)。在ConfigProvider类的配置数组的templates键中添加条目。

  • 如何在Zend Expressive中处理错误? Zend Expressive包含默认错误处理中间件。您可以创建自定义中间件来处理错误。

  • 如何测试Zend Expressive应用? 使用PHPUnit。

  • 如何部署Zend Expressive应用? 像任何其他PHP应用一样部署,可以使用Apache、Nginx或PHP内置服务器。

  • 哪里可以找到更多关于Zend Expressive的资源? Zend Framework官方网站、Zend Expressive文档和Zend Framework社区论坛。

以上是Zend表达模块的快速发展的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板