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.php
的getDependencies()
方法中添加以下代码:
'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和模板代码与原文相同,此处省略,请参考原文。)
创建、编辑和删除功能的实现留作练习。
总结
本教程展示了使用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中文网其他相关文章!

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

记事本++7.3.1
好用且免费的代码编辑器