yii2 高级应用程序模板

tech2022-09-11  97

yii2 高级应用程序模板

I’ve made it no mystery that I work with Yii and enjoy using it. I’m somewhat of an old school guy and don’t mind reading a book to get started. I like the way they normally give you a gradual introduction to something you want to gain knowledge about. It’s a more subtle way to start with something and you don’t immediately drown in the huge amount of information.

与Yii一起工作并喜欢使用它已不是什么难事。 我有点像个高中生,不介意读书。 我喜欢他们通常向您逐步介绍您想获得的知识的方式。 这是从某件事开始的一种更微妙的方法,您不会立即被大量信息淹没。

That’s the way I started on Yii 1.1 some years ago. Unfortunately, I tossed the book I used back then half way through; it went too slow for me. Going over simple things repeatedly made me lose my interest quite fast. I wanted to learn more, faster. I had a whole bunch of ideas in my head and I wanted to find out how to realize them.

那就是几年前我开始使用Yii 1.1的方式。 不幸的是,我把我曾经用过的那本书扔了一半。 对我来说太慢了 反复研究简单的事情使我很快失去了兴趣。 我想学更多,更快。 我脑子里有很多想法,我想知道如何实现它们。

We’re a few years down the road and with the introduction of Yii 2.0 I let myself be tempted yet again by a book before starting some serious work with Yii 2.0. With mixed experiences in mind I started on a copy of Web Application Development with Yii 2 and PHP and ended up pleasantly surprised.

我们已经走了几年,随着Yii 2.0的推出,在开始使用Yii 2.0进行一些认真的工作之前,我再次被一本书所吸引。 考虑到混合的经验,我开始使用Yii 2和PHP复制Web应用程序开发,并最终感到惊喜。

内容 (Content)

The structure of this 2.0 version is somewhat similar to the previous one (which caused Jeffrey Winesett’s name to be on the cover) but this one is entirely written by Mark Safronov.

这个2.0版本的结构与先前的版本(导致Jeffrey Winesett的名字被掩盖)有些相似,但是此版本完全由Mark Safronov编写。

Having had some experience with Yii, I was looking for the differences and an insight into how you build an application from nothing.

在拥有Yii的经验之后,我一直在寻找差异,并了解如何从零开始构建应用程序。

The book does this, as usual, by taking an example application and gradually extending it. It starts with a test-first approach. If you feel serious about your application and maintaining it long-term, you’ll probably agree that tests are a vital part. Luckily, this approach relaxes quite fast, not spelling out how to define a test every time something new is added.

与往常一样,本书通过举一个示例应用程序并逐步扩展它来完成此任务。 它从测试优先的方法开始。 如果您对应用程序认真对待并长期维护它,那么您可能会同意测试是至关重要的部分。 幸运的是,这种方法放宽了速度,没有在每次添加新内容时都说明如何定义测试。

The book covers all the key components of the framework; rendering, authentication, authorization, modules, behavior, extensions, events, grid view and route management. All the components are reviewed along with how to use them. It also dives into the working of the framework quite often, showing the logic in it and explaining in detail how they work.

本书涵盖了框架的所有关键组成部分。 渲染,身份验证,授权,模块,行为,扩展,事件,网格视图和路由管理。 审查了所有组件以及如何使用它们。 它还经常深入研究框架的工作,展示框架的逻辑并详细解释框架的工作原理。

It has a slight touch of humor every now and then, making it a very easy read.

时不时有一点幽默感,使其很容易阅读。

I did, again, skip some pages right around modules and the behavior part, but that was more because I was very interested in seeing what was coming in the chapters after that. Ever since I finished the book and started on my first real Yii 2.0 project, I’ve already gone back to the book a few times. I came across a few new things and remembered some of the nice suggestions that were made for improvement. For me, that was proof enough that it was at least worth the read.

我的确再次跳过了模块和行为部分的某些页面,但这更多是因为我对接下来的章节中的内容非常感兴趣。 自从我完成本书并开始我的第一个真正的Yii 2.0项目以来,我已经回过几次书了。 我遇到了一些新情况,并想起了一些改进建议。 对我来说,这足以证明至少值得一读。

面试 (Interview)

Being left with quite a good feeling about this book, I went to contact Mark Safronov for some Q&A on the book and Yii itself.

对这本书感觉很好,我去联系了马克·萨夫罗诺夫,就这本书和Yii进行了一些问答。

您能告诉我们您的背景吗,您是谁? (Can you tell us a bit about your background, who are you?)

There’s not much to brag about. I am a full-time developer in Clevertech, and I am Russian, living in a country where horses ride YOU.

没什么可吹嘘的。 我是Clevertech的全职开发人员,我是俄罗斯人,居住在一个骑马的国家。

I am working on a website based on Yii 1.1 every day and have been doing it for more than two years already. This website is sufficiently large and complex to really test the system-building skills of anyone deeply involved in the development, with the requirements changing on daily basis. So I dare to consider myself knowing thing or two about building and maintaining real-world Yii-based Web applications.

我每天都在基于Yii 1.1的网站上工作,并且已经从事了两年以上。 该网站足够大且复杂,可以真正测试参与开发的任何人的系统构建技能,并且需求每天都在变化。 因此,我敢于考虑自己对构建和维护基于实际Yii的Web应用程序的一两件事。

In the meantime, I maintained the opensource YiiBooster and rewrote from scratch the YiiBoilerplate project.

同时,我维护了开源YiiBooster并从头重写了YiiBoilerplate项目。

I have been poisoned by PHP for around 5 years already. I started my career in a Russian Web studio which made websites for its customers based on the dreaded 1C-Bitrix CMS. You westerners probably don’t know this Frankenstein of CMS-es but I certainly do regret knowing it.

我已经被PHP毒死了大约5年。 我的职业生涯始于一家俄罗斯网络工作室,该工作室基于可怕的1C-Bitrix CMS为客户创建网站。 你们西方人可能不了解CMS-es的Frankenstein,但我当然会后悔。

Over the years, I switched my habits from reading sci-fi and fantasy novels to reading hardcore fundamental technology books like Working Effectively with Legacy Code, GooS, DDD and such. This has proven itself a lot more entertaining, if not enlightening.

多年以来,我的习惯从阅读科幻小说和幻想小说转变为阅读核心基础技术书籍,例如有效地使用Legacy Code,GooS,DDD等。 事实证明,这很有娱乐性,甚至没有启发性。

您最终是如何写这本书的? (How did you end up writing this book?)

I have co-authored another title, “INSTANT Yii 1.1 Application Development Starter”. At that time, the publisher lost the original author (literally lost, they could not contact him after he sent them the drafts) and chose me, because they already had me as a reviewer for that title.

我与他人合着了另一本书“ INSTANT Yii 1.1 Application Development Starter”。 那时,出版商失去了原作者(字面上是失去的,在他将草稿发送给他们后,他们无法与他联系)并选择了我,因为他们已经让我担任该标题的审阅者。

So, we already had some cooperation prior to this book and they probably decided that I write convincingly.

因此,在本书编写之前我们已经进行了一些合作,他们可能认为我写的很有说服力。

为什么选择Yii 2.0,因为框架列表很长? (Why Yii 2.0, since the list of frameworks is quite long?)

First of all, I got no other choice; the publisher gave me the offer specifically for a book about Yii. And Yii is the framework I am dealing with on a daily basis.

首先,我别无选择。 出版商给了我一个专门针对Yii的书的要约。 Yii是我每天都要处理的框架。

Also, version 2 is an obvious next step. I saw this as an opportunity to learn about the intricacies of its internals. On a project I am currently working on it’s a common practice to delve into the source code of the framework itself, because you frequently need absolutely exact knowledge of how a method (like CController.render() or CWebUser.login()) works.

同样,版本2是显而易见的下一步。 我认为这是一个了解其内部复杂性的机会。 在当前正在研究的项目中,探究框架本身的源代码是一种常见的做法,因为您经常需要对方法(如CController.render()或CWebUser.login() )的工作方式有绝对的了解。

I do need to make a major disclaimer here. I am not biased towards Yii in any way. To be honest, I actually dislike it, but bear with it at the same time. I have no attachment and only a strictly professional relation to this framework. This certainly affected both the overall style of the book and the areas it covers.

我确实需要在此声明一个主要的免责声明。 我丝毫不偏爱Yii。 老实说,我实际上不喜欢它,但要同时忍受。 我没有任何依恋,并且对此框架只有严格的专业关系。 这无疑影响了本书的整体风格及其涵盖的领域。

您觉得Yii 1.1的弱点是什么? (What do you feel was the weak point of Yii 1.1?)

The most important pain point of Yii was, and still is, the Service Locator, which is, incidentally, the central idiom of the whole framework. If you are accustomed to sticking calls to Yii::$app everywhere through your whole application, then I guarantee you, you will experience major butthurt later on, when your project grows. Even earlier if you try to auto-test it.

Yii最重要的痛点是,现在仍然是服务定位器,顺便说一句,它是整个框架的中心用法。 如果您习惯于在整个应用Yii::$app随处挂起对Yii::$app调用,那么我向您保证,当您的项目扩展时,您将在以后遇到重大的挑战。 如果您尝试对其进行自动测试,那就更早了。

Yii 2 tried to overcome it by shipping the DI container inside, but it has almost no coverage in documentation, every tutorial assumes you do Yii::$app everywhere. Due to my enormous mental inertia, I have not covered yii\di\Container in the book, which I regret.

Yii 2试图通过将DI容器运送到其中来克服它,但是它在文档中几乎没有涉及,每个教程都假设您在各处都使用Yii::$app 。 由于我巨大的精神惰性,我没有在书中介绍yii\di\Container ,对此我感到遗憾。

A second issue is its tight coupling to the classes. Most of the time, when you need to meddle with something built-in to Yii, you need to both subclass what you need and copy the old behavior from superclass.

第二个问题是它与班级的紧密联系。 大多数时候,当你需要的东西染指内置在Yii中,你需要在两个子类,你需要什么,并从超类复制旧的行为。

Yii 2 has a lot of improvements here. But nevertheless; try to write the custom Column class which has automatic calculation of totals in the footer, without copy-pasting parts of the Column class.

Yii 2在这里有很多改进。 但尽管如此; 尝试编写自定义的Column类,该类具有自动计算页脚中的总数,而无需复制粘贴Column类的某些部分的功能。

确实大大促进了Yii::$app的使用。 从长远来看,为什么使用它会对我们造成伤害? (The use of Yii::$app is quite promoted indeed. Why would using it hurt us in the long run exactly?)

The reason is very simple: as long as you have a Yii::app()->something call in your class, you can’t test it in isolation; it will be the integrated test, because you need to include the YiiBase class and, actually, your application config, autoloader and also you need to start the Yii application because you need your components attached and configured.

原因很简单:只要您在类中有一个Yii::app()->something调用,就不能孤立地对其进行测试。 这将是集成测试,因为您需要包括YiiBase类,并且实际上包括您的应用程序配置,自动加载器,并且还需要启动Yii应用程序,因为您需要连接和配置组件。

If you don’t see it as a pain, all right, but I personally think that even in terms of run time alone this is a totally failed approach. Gary Bernhardt says that even 100ms test runtime is too much and I usually prefer to listen to people of his caliber. And running time is not the only problem with such tight coupling.

如果您不觉得这很痛苦,那么可以,但是我个人认为,就运行时间而言,这是完全失败的方法。 加里·伯恩哈特( Gary Bernhardt) 说,即使100毫秒的测试运行时间也太长了,我通常更喜欢听他的才能。 运行时间并不是这种紧密耦合的唯一问题。

那我们该用什么呢? 以及使用这种方式如何影响自动化测试? (What should we use, then? And how would using this influence automated tests?)

Dependency Injection techniques, in particular, separating the creating of the application object graph and actually performing processing. As far as I know, people do this by using some kind of IoC container, but I have no actual first-hand experience working with them as the project I am working on on daily basis is too legacy to even try to use such things there.

依赖注入技术尤其是将应用程序对象图的创建与实际执行的处理分开。 据我所知,人们通过使用某种IoC容器来做到这一点,但是我没有与他们合作的实际第一手经验,因为我每天从事的项目都太过传统,以至于无法在那里尝试使用此类东西。 。

I have already said above how it influences automated tests. In short, we have the Law of Demeter for years now, and sticking to it actually works (with this I do have first-hand experience) and leads to decoupled, maintainable, testable design.

上面我已经说过它如何影响自动化测试。 简而言之,我们已经有多年的《得墨the耳法则》,并且坚持下去实际上是行得通的(我确实有第一手的经验),并且可以实现脱钩,可维护,可测试的设计。

您说Yii 2“试图克服”这一点。 用这种方式表述意味着他们没有成功。 (You say that Yii 2 “tried to overcome” this. Formulating it that way implies that they didn’t succeed.)

They would succeed by completely throwing the Yii singleton away and making everything build itself decoratively in the index.php entry point. This way we would actually work with Yii in a TDD manner. I dare to spell my personal opinion: I am reading professional literature every single day trying to catch up with all the community knowledge I missed while wasting time in university, and sometimes I think that the percentage of people that actually care about problems already solved is extremely low; the majority of the community is just spitting out code in code-fix and write-forget manner thinking that there’s no way to do better. I haven’t seen the techniques described in “DDD” by Eric Evans actually implemented ever and I find this completely unbelievable. I tried to use what I learned in the course of the “Web app development with Yii 2” but I consider myself failed at this approach, as I have too little experience working in such a way.

他们将完全放弃Yii单例,并使所有内容都在index.php入口点以装饰性方式构建,从而获得成功。 这样,我们实际上将以TDD方式与Yii合作。 我敢于拼写出自己的个人观点:我每天都在阅读专业文学,以赶上我在浪费时间上大学时所错过的所有社区知识,有时我认为实际上关心已经解决的问题的人们所占的百分比是极低 大多数社区只是以代码修复和忘记写入的方式吐出代码,认为没有办法做得更好。 我还没有见过Eric Evans在“ DDD”中描述的技术实际上从未实现过 ,我发现这简直令人难以置信。 我尝试使用在“使用Yii 2进行Web应用程序开发”过程中所学到的知识,但是由于这种方法的工作经验太少,我认为自己在这种方法上失败了。

既然您对Yii有相当的经验,那么您对2.0版中的更改有何总体看法? 这如何影响我们的工作方式? (Since you have quite a bit of experience with Yii, what is your general feeling about what changed in version 2.0? And how does that influence the way we can work with it?)

I was very pleased with the improvements in the Yii 2 framework. It was a physical feeling of pleasure sometimes.

我对Yii 2框架的改进感到非常满意。 有时是一种身体上的愉悦感。

They use namespaces, which is just great, as I have namespace seams now and can have classes with identical names through my project;

他们使用名称空间,这很好,因为我现在有了名称空间接缝,并且可以在我的项目中使用具有相同名称的类。 There are interfaces now in critical places, like DataProviderInterface, which means that I can provide my own implementation without the need to subclass the whole huge CComponent.

现在在关键位置有接口,例如DataProviderInterface,这意味着我可以提供自己的实现,而无需将整个庞大的CComponent子类化。

On the other hand, they subclass everything from the custom-made Object class, but this class is actually a nice invention – it’s the stdClass with automatic getSomething() and setSomething() methods, configurable by the associative configuration array and some other features. At the same time, this class uses a static call to Yii::configure() right inside its constructor which, again, reverts the dependencies inside out.

另一方面,它们从定制的Object类getSomething()所有子类,但该类实际上是一个不错的发明–它是具有自动getSomething()和setSomething()方法的stdClass,可通过关联配置数组和某些其他功能进行配置。 同时,此类在其构造函数内部使用对Yii::configure()的静态调用,该调用再次将内部的依赖关系还原出来。

They finally extracted some entities as separate components, like Response and View. The Response concept is amazing, as we can finally implement sane CSV and JSON formatting in an idiomatic way and without crazy hacks involving sub classes again.

他们最终提取了一些实体作为单独的组件,例如Response和View。 Response的概念令人惊奇,因为我们最终可以以惯用的方式实现理智的CSV和JSON格式,而无需再次疯狂地涉及子类。

They actually have a dependency injection container in the code base, so we can finally hope to decrease the usage of calls to Yii::$app through the code base.

它们实际上在代码库中有一个依赖项注入容器,因此我们最终可以希望减少通过代码库对Yii::$app的调用的使用。

This is just what I can recall right now. There is absolutely no need to keep sticking to Yii 1.1 if you can migrate. However, I am pretty sure nobody will be able to migrate existing Yii 1.1 application to Yii 2 without substantial rewriting of most of the application.

这就是我现在记得的东西。 如果可以迁移,绝对没有必要坚持使用Yii 1.1。 但是,我敢肯定,如果不对大多数应用程序进行实质性的重写, 没有人能够将现有的Yii 1.1应用程序迁移到Yii 2。

在整本书中,我多次注意到您引用了Yii网站上有关某些主题的“优秀”文档。 它对书籍研究有帮助吗? (I noticed several times throughout the book that you refer to the ‘excellent’ documentation on the Yii website regarding some subjects. Did it help in the book research?)

Honestly, I admire the amount and quality of documentation in Yii, regardless of my overall feelings about it. As a developer using the software which should be the root for my work, I usually need precise information about the tiniest details, for example, contracts needed to be followed when calling a particular function. Especially when we’re quite far from the type safety of compiled languages. When I praise its documentation I usually mean its self-documentation, its docblocks in code.

老实说,无论我对此有何总体看法,我都钦佩Yii中文档的数量和质量。 作为使用该软件的开发人员,我通常需要有关最微小细节的精确信息,例如,调用特定功能时需要遵守合同。 尤其是当我们离编译语言的类型安全性很远时。 当我称赞它的文档时,我通常指的是自我文档 ,即代码中的docblock。

Yii 1.1 was enormously hard for me to understand back when I just started to understand OOP, but with time, all the pieces came into place finally and I figured out its structure. Yii 2 doesn’t have a lot of significant changes in architecture so it was really easy to research.

当我刚开始了解OOP时,Yii 1.1很难理解,但是随着时间的流逝,所有的部分终于都准备就绪,我弄清楚了它的结构。 Yii 2在架构上没有太多重大变化,因此研究起来真的很容易。

您对将代码从ActiveRecord ORM层解耦做出了坚定的声明。 您能告诉我们为什么要这样做吗? (You make a firm statement about decoupling your code from the ActiveRecord ORM layer. Can you tell us why we would want to do this?)

The main reason for this lies in the object-relation impedance mismatch. At one point in time I realized that my logical domain model does not fit into a single table and so, ActiveRecord suddenly became a limiting factor. More than that, the whole set of features stuffed into them is completely unnecessary in the domain layer, where the actual business-related work happens. Shortly after that I learned about the object-relation impedance mismatch, Repository pattern, CQRS, and became completely convinced that the place of ORM should be at the lowest possible layer of the architecture, even lower than the actual data access layer.

其主要原因在于对象关系阻抗不匹配 。 在某个时间点,我意识到我的逻辑域模型不适合单个表,因此ActiveRecord突然成为限制因素。 不仅如此,在实际与业务相关的工作发生的领域层中,完全不需要填充全部功能。 此后不久,我了解了对象关系阻抗不匹配,存储库模式CQRS,并完全确信ORM的位置应位于体系结构的最低层,甚至应低于实际数据访问层。

This book however is about Yii, not general development. I could not gloss over the problem of having people calling ActiveRecords right from Controllers so I have shown the simplest possible decoupling method in chapter 2.

但是这本书是关于Yii的,而不是一般的发展。 我无法掩盖让人们直接从Controllers调用ActiveRecords的问题,因此我在第2章中展示了最简单的解耦方法。

当我读到以下内容时,我笑了起来:“在网络覆盖的,基于PHP的世界中,没有习惯使用像XDebug这样的工具来窥探各个变量,就像戴着C恤的C程序员那样。” 我开始怀疑你是否认真对待这一点。 您根本不使用调试器吗? (I had a laugh when I read: “In the web-covered, PHP-based world there’s no habit to use such tools as XDebug, to peek into the individual variables, like beard-wearing C programmers do”. I started to wonder if you were serious about this. You don’t use a debugger at all?)

I admit, I spelled out my personal and my team’s opinion. There are probably a lot of people who use xdebug routinely and it helps them. I personally have found that when I am able to write automated tests, I simply don’t need the debugger at all, even if I don’t test first. Any data flow check I can manually perform with a debugger can be encoded as auto test at some level (ideally a unit one) and it will serve both as a regression test in future and the documentation example for others. A total win. And var_dump(); die() when you’re hacking away is just quicker. Maybe I am just too dumb to use xdebug, though :)

我承认,我阐明了我个人和团队的观点。 可能有很多人定期使用xdebug,它可以帮助他们。 我个人发现,当我能够编写自动化测试时,即使我没有先进行测试,我也根本不需要调试器。 我可以使用调试器手动执行的任何数据流检查,都可以在某种程度上被编码为自动测试(理想情况下是一个单元测试),并且将来既可以用作回归测试,也可以用作其他文档的示例。 共赢。 还有var_dump(); die() 黑客入侵时, var_dump(); die()更快。 也许我太笨了,无法使用xdebug :)

结论 (Conclusion)

In our conversation, Mark makes a side note which I’d like to close off with;

在我们的对话中,Mark作了一个旁注,我想以此作为结尾。

…if you’re still not sure about whether you need to migrate to Yii 2, be it from Yii 1.1 or from bare procedural PHP or from some CMS, then my answer is: if you are able to, then do it immediately. Yii 2 is an enormous leap in overall quality and expressibility. It builds upon virtually everything good which emerged in the PHP community over the recent years and you certainly don’t want to miss it.

…如果您仍然不确定是否需要从Yii 1.1迁移到Yii 2,或者从裸程序PHP或从某些CMS迁移到Yii 2,那么我的回答是:如果有能力,请立即进行。 Yii 2是整体质量和可表达性的巨大飞跃。 它建立在近几年PHP社区中出现的几乎所有优点的基础上,您当然不想错过它。

I’d like to thank Mark for the time he took to answer my questions and the insights he gave into software development as a whole.

我要感谢Mark花费的时间回答我的问题以及他对整个软件开发的见解。

翻译自: https://www.sitepoint.com/interview-mark-safronov-author-web-app-development-yii-2/

yii2 高级应用程序模板

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)