Zend Expressive模块快速开发指南:构建只读博客模块
本文将分享一些Zend Expressive模块开发技巧,帮助您快速构建一个功能完善的只读博客模块。请确保您已按照之前的教程设置好开发环境,包括安装和配置Zend Expressive、Doctrine、Gulp以及抽象反射工厂(约需10分钟)。
在本教程中,我们将快速构建一个简单的只读博客模块(从数据库中列出博客文章),展示Zend Expressive的快速开发能力。
模块设置
在您的Expressive应用中运行以下命令:
<code class="language-bash">./vendor/bin/expressive module:create Blog</code>
这将生成Blog模块的基本代码,并自动将其注册到您的应用程序和Composer自动加载器中。
Doctrine实体和数据库表
接下来,创建Blog实体和数据库表。首先,我们需要让应用程序知道该模块提供了Doctrine实体。
打开src/Blog/src/ConfigProvider.php
,添加以下代码:
<code class="language-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', ], ], ], ]; }</code>
在src/Blog/config/doctrine
目录下创建BlogPost.orm.yml
文件,内容如下:
<code class="language-yaml">--- 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</code>
运行./vendor/bin/doctrine orm:generate-entities src
。 由于Doctrine不支持PSR-4标准的目录结构,需要将src/Blog/Entity
移动到src/Blog/src/Entity
。 然后运行以下命令创建数据库表:
<code class="language-bash">./vendor/bin/doctrine orm:schema-tool:create</code>
您可以运行以下SQL语句填充数据库表:
<code class="language-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.');</code>
路由设置
Expressive模块不直接注册路由。我们需要使用一个小技巧来实现:创建src/Blog/src/Factory/RoutesDelegator.php
文件,内容如下:
<code class="language-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; } }</code>
在src/Blog/src/ConfigProvider.php
的getDependencies()
方法中添加以下代码:
<code class="language-php">'delegators' => [ \Zend\Expressive\Application::class => [ Factory\RoutesDelegator::class, ], ],</code>
创建src/Blog/config/routes.php
文件,添加博客路由:
<code class="language-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');</code>
Action和模板
接下来,创建Action来处理路由请求,并创建模板文件。 (Action和模板代码与原文相同,此处省略,请参考原文。)
创建、编辑和删除功能的实现留作练习。
总结
本教程展示了使用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中文网其他相关文章!