Introduction (Excerpt)

This is an excerpt from "The Yii Book" by Larry Ullman. This particular excerpt is most of the Introduction. Use the links above to learn more about, or buy, this book.

This is the 24th book that I've written, and of the many things I've learned in that time, a reliable fact is this: readers rarely read the introduction. Still, I put a fair amount of time into the introduction and would ask you to spend the five minutes required to read it.

In this particular introduction, I provide the arguments for (and against) frameworks, and the Yii framework specifically. I also explain what knowledge and technical requirements are expected of you, the dear reader. And if that was not enough, the introduction concludes by providing you with resources you can use to seek help when you need it.

So: five minutes of your time for all that. Okay, maybe 8 minutes. How about you give it a go?

Why Frameworks?

Simply put, a framework is an established library of code meant to expedite software development. Writing everything from scratch on every project is impractical; code reuse is faster, more reliable, and possibly more secure.

Many developers eventually create a lightweight framework of their own, even if that's just a handful of commonly used functions. True frameworks such as Yii are just the release of a complete set of tools that a smart and hardworking person has been kind enough to make public. Even if you don't buy the arguments for using a framework in its own right, it's safe to say that the ability to use a framework, whether that means a few pieces of your own reusable code or a full-fledged framework such as Yii, is to be expected for any regular programmer today.

Why You Should Use a Framework

The most obvious argument for using a framework is that you'll be able to develop projects much, much faster than if you didn't use a framework. But there are other arguments, and those are more critical.

As already stated, framework-based projects should also be both more reliable and secure than one coded by hand. Both qualities come from the fact that framework code will inevitably be far more thoroughly tested than anything you create. By using a framework, with established code and best practices, you're starting on a more stable, secure, and tested foundation than your own code would provide (in theory).

Similarly, a framework is likely to impose a quality of documentation that you might not take the time to implement otherwise. The same can go for other professional features, such as logging and error reporting. These are features that a good framework includes but that you may not get around to doing, or doing properly, despite your best intentions.

Still, the faster development argument continues to get the most attention. If you are like, well, almost everyone, your time is both limited and valuable. Being able to complete a project in one-third the time means you can do three times the work, and make three times the money. In theory.

You can also make more money when you know a framework because it improves your marketability. Framework adoption is almost a must for team projects, as frameworks impose a common development approach and coding standard. For that reason, most companies hiring new Web developers will expect you to know at least one framework.

In my mind, the best argument for using a framework is this: so that you can always choose the right tool for the job. Not to be cliché, but I firmly believe that one of the goals of life is to keep learning, to keep improving yourself, no matter what your occupation or station. As Web developers in particular, you must continue to learn, to expand your skill set, to acquire new tools, or else you'll be left behind. Picking up a framework is a very practical choice for your own betterment. In fact, I would recommend that you actually learn more than one framework. By doing so, you can find the right framework for you and better understand the frameworks you know (just as I understood English grammar much better only after learning French).

Why You Shouldn't Use a Framework

If frameworks are so great, then why isn't everyone using a framework for every project? First, and most obviously, frameworks require extra time to learn. The fifth project you create using a framework may only take one-third the time it would have taken to create the site from scratch, but the first project will take at least the same amount of time as if you had written it from scratch, if not much longer. Particularly if you're in a rush to get a project done, the extra hours needed to learn a framework will not seem like time well spent. Again, eventually frameworks provide for much faster development, but it will take you a little while to get there.

Second, frameworks will normally do about 80% of the work really easily, but that last 20%---the part that truly differentiates this project from all the others---can be a real challenge. This hurdle is also easier to overcome the better you know a framework, but implementing more custom, less common Web tasks using a framework can really put you through your paces.

Third, from the standpoint of running a Web site or application, frameworks can be terribly inefficient. For example, to load a single record from a database, a framework may require three queries instead of just the one used by a conventional, non-framework site. As database queries are one of the most expensive operations in terms of server resources, three times the queries is a ghastly thought. And framework-based sites will require a lot more memory, as more objects and other resources are constantly being created and used.

{NOTE} Frameworks greatly improve your development time at a cost of the site's performance.

That being said, there are many ways to improve a site's performance, and not so many ways to give yourself back hours in the day. More importantly, a good framework like Yii has built-in tools to mitigate the performance compromises being made. In fact, through such tools, it's entirely possible that a framework-based site could be more efficient than the one you would have written from scratch.

Fourth, when a site is based upon a framework, you are expected to update the site's copy of the framework's files (but not the site code itself) as maintenance and security releases come out. This is true whenever you use third-party code. (Although, on the other hand, this does mean that other people are out there finding, and solving, potential security holes, which won't happen with your own code.)

How You Use a Framework

Once you've decided to give framework-based programming a try, the next question is: How? First, you have to have a solid understanding of how to develop without using a framework. Frameworks expedite development, but they only do so by changing the way you perform common tasks. If you don't, for example, understand basic user interactions in conventional Web pages, then switching to using a framework will be that much more bewildering.

And second, you should give in to the framework. All frameworks have their own conventions: how things are to be done. Attempting to fight those conventions will be a frustrating, losing battle. Do your best to accept the way that the framework does things and it'll be a smoother, less buggy, and faster experience.

Why Yii?

The Yii framework was created by Qiang Xue and first released in 2008. "Yii" is pronounced like "Yee", and is an acronym for "Yes, it is!". From Yii's official documentation:

Is it fast?...Is it secure?...Is it professional?...Is it right for my next project?...Yes, it is!

"Yii" is also close to the Chinese character "Yi", which represents easy, simple, and flexible.

Mr. Xue was also the founder of the Prado framework, which took its inspiration from the popular ASP.NET framework for Windows development. In creating Yii, Mr. Xue took the best parts of Prado, Ruby on Rails, CakePHP, and Symfony to create a modern, feature-rich, and very useable PHP framework.

At the time of this writing, the current, stable release of the Yii framework is 1.1.13. It is expected that version 2 of the Yii framework will have its alpha release in early 2013.

What Yii Has to Offer

Being a framework, Yii offers all the strengths and weaknesses that frameworks in general have to offer (as already detailed). But what does Yii have to offer as a framework in its own right?

Like most frameworks, Yii uses pure Object-Oriented Programming (OOP). Unlike some others, Yii has always been designed to only work with PHP 5, which has a significantly improved and advanced object structure compared with the older PHP 4 (let alone the archaic and rather lame object model that existed way back in PHP 3). For me, frameworks that were not written specifically for PHP 5 and greater aren't worth considering.

Yii uses the de facto standard Model-View-Controller (MVC) architecture pattern. If you're not familiar with it, Chapter 1, "Fundamental Concepts," explains this approach in detail.

Almost all Web applications these days rely upon an underlying database. Consequently, how a framework manages database interactions is vital. Yii can work with databases in several different ways, but the common approach is through Object Relational Mapping (ORM) via Active Record (AR). If you don't know what ORM and AR are, that's fine: you'll learn well enough in time. The short description is that an ORM handles the conversion of data from one source to another. In the case of a Yii-based application, the data will be mapped from a PHP object variable to a database record and vice versa.

{TIP} The excellent Ruby on Rails framework also uses Active Record for its database mapping.

For low-level database interactions, Yii uses PHP 5's PHP Data Objects (PDO). PDO provides a data-access abstraction layer, allowing you to use the same code to interact with the database, regardless of the underlying database application in use.

One of Yii's greatest features is that if you prefer a different approach, you can swap alternatives in and out. For example, you can change:

  • The underlying database-specific library
  • The template system used to create the output
  • How caching is performed
  • And so forth

The alternatives you swap in can be code of your own creation, or that found in third-party libraries, including code from other frameworks!

Despite all this flexibility, Yii is still very stable, and through caching and other tools, can be tweaked to perform quite well. Yii applications can scale well, as has been tested on some high-demand sites, such as Stay.com and VICE.

All that being said, many of Yii's benefits and approaches apply to other PHP frameworks as well. Why you should use Yii is far more subjective than a list of features and capabilities. At the end of the day, you should use Yii if the framework makes sense to you and you can get it to do what you need to do.

{NOTE} For a full sense of Yii's feature set, see this book's table of contents online or the features page at the official Yii site.

As for myself, I initially came to Yii because it requires PHP 5---I find backwards-compatible frameworks to be inherently flawed---and uses the jQuery JavaScript framework natively. (By comparison, the widely-used Zend Framework was rather slow to adopt jQuery, in my opinion.) I also love that Yii will auto-generate a ton of code and directories for you, a feature that I had come to be spoiled by in Rails. Yii is also well-documented, and has a great community. Mostly, though, for me, Yii just feels right. And unless you really investigate a framework's underpinnings to see how well designed it is, how the framework feels to you is a large part of the criteria in making a framework selection.

In this book and my blog, I'm happy to discuss what Yii has to offer: why you should use it. The question I can't really answer is what advantage Yii has over this or that framework. If you want a comparison of Yii vs. X framework, search online, but remember that the best criteria for which framework you should use is always going to be your own personal experience.

The only other PHP framework I've used extensively is Zend. The Zend Framework has a lot going for it and is worth anyone's consideration. To me, its biggest asset is that you can use it piecemeal and independently (I've often used components of the Zend Framework in Yii-based and non-framework-based sites), but I just don't like the Zend Framework as the basis of an entire site. It requires a lot of work, the documentation is overwhelming while still not being that great, and it just doesn't "feel" right to me.

I really like the Yii framework and hope you will too. But this book is not a sales pitch for using Yii over any other framework, but rather a guide for those needing help.

Who Is Using Yii?

The Yii framework has a wide international adoption, with extensive usage in:

  • United States
  • Russia
  • Ukraine
  • China
  • Brazil
  • India
  • Europe

Many open-source apps have been written in Yii, including:

What's New In Yii 2?

In early 2013, the alpha release of Yii 2 should come out, with the general release coming later in 2013. At the time of this writing, the major changes for Yii 2 are still under consideration, but it is known that Yii 2 will:

  • Use namespaces for its classes
  • Have a more logical structure for its MVC components
  • Do more for creating console applications
  • Make improvements for working with databases, including Active Record changes
  • All in all, be even more beautiful (truly!)

What You'll Need

Learning any new technology comes with expectations, and this book on Yii is no different. I've divided the requirements into two areas: technical and personal knowledge. Please make sure you meet the bar on both before getting too far into the book.

Technical Requirements

Being a PHP framework, Yii obviously requires a Web server with PHP installed on it. Version 1 of the Yii framework requires PHP 5.1 or greater. Version 2 is expected to require PHP 5.3 or later. At the time of this writing, the latest version of PHP is 5.4.8. This book will assume you're using Apache as your Web server application. If you're not, see the Yii documentation or search online for alternative solutions when Apache-specific options are presented.

{NOTE} In my opinion, it's imperative that Web developers know what versions they are using (of PHP, MySQL, Apache, etc.). If you don't already, check your versions now.

You'll also want a database application, although Yii will work with all the common ones. This book will primarily use MySQL, but, again, Yii will let you easily use other database applications with only the most minor change to your code.

All of the above will come with any decent hosting package. I expect all developers to install a Web server and database application on their own desktop computer though: it's the standard---and far easier---way to create Web sites. Oh, and it's all free! If you have not yet installed an *AMP stack---Apache, MySQL, and PHP---on your computer, I would recommend you do so now. The most popular solutions are:

All of these are free.

To write your code, you'll also need a good text editor or IDE. In theory, any application will do, but you may want to consider one that directly supports Yii, or can be made to support Yii. That list includes (all information is correct at the time of this writing; all prices in USD):

"Support" really means recognition for keywords and classes particular to Yii, the ability to perform code completion, and potentially even support Yii-specific wizards.

In case you're curious, I almost exclusively use a Mac, and currently use the excellent TextMate text editor (only for Mac, $51).

Your Knowledge and Experience

There are not only technical requirements for this book, but also personal requirements. In order to follow along, it is expected that you:

  • Have solid Web development experience
  • Are competent with HTML, PHP, MySQL, and SQL
  • Aren't entirely uncomfortable with JavaScript and jQuery
  • Understand that confusion and frustration are a natural consequence of learning anything new (although I'll do my best in this book to minimize the occurrence of both)

The requirements come down to this: using a framework, you'll be doing exactly the kinds of things you have already been doing, just using a different methodology. Learning to use a framework is therefore the act of translating the conventional approach into a new approach.

The book does not assume mastery of Object-Oriented Programming, but things will go much more smoothly if you have prior OOP experience. Chapter 1 hits the high notes of OOP in PHP regardless.

About This Book

Most of this introduction is about frameworks in general and the Yii framework in particular, but I want to take a moment to introduce this book as a whole, too.

The Goals of This Book

I had two goals in writing this book. The first is to explain the entirety of the Yii framework in such a way as to convey a sense of the big picture. In other words, I want you to be able to understand why you do things in certain ways. By learning what Yii is doing behind the scenes, you will be better able to grasp the context for whatever bits of code you'll end up using on your site. This holistic approach is what I think is missing among the current documentation options.

The second goal is to demonstrate common tasks using real-world examples. This book is, by no means, a cookbook, or a duplication of the Yii wiki, but I would be remiss not to explain how you implement solutions to standard Web site needs. In doing so, though, I'll explain the solutions within the context of the bigger picture, so that you walk away not just learning how to do X but also why you do it in that manner.

All that being said, there are some things relative to the Yii framework (and Web development in general) that the book will not cover. For example, Yii 1 defines many of its own data types, used in more advanced applications. Some of these are replicated in PHP's Standard PHP Library, which will be used in Yii 2 instead. This book omits coverage of them, along with anything else I've deemed equally esoteric.

Still, my expectation is that after reading this book, and understanding how the Yii framework is used, you'll be better equipped to research and learn these omissions, should you ever have those needs.

How I Wrote This Book

For those of you that care about such things, this book was written using the Scrivener application running on Mac OS X. Scrivener is far and away the best writing application I've ever come across.

Images were taken using Snapz Pro X.

The entire book was written using MultiMarkdown, an extension of Markdown. The MultiMarkdown source was then converted to various formats using:

{NOTE} To be precise, Pandoc itself uses a slight variation of Markdown.

About Larry Ullman

I am a writer, developer, consultant, trainer, and public speaker. This is my 24th book, with the vast majority of them related to Web development. My PHP for the Web: Visual QuickStart Guide and PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide books are two of the bestselling guides on the PHP programming language. Both are in their fourth editions, at the time of this writing. I've also written Modern JavaScript: Develop and Design, which is thankfully getting excellent reviews.

I first started using the Yii framework in 2009, a few months after the framework was publicly released. Later that year, I wrote my "Learning the Yii Framework" series on my blog, which has become quite popular. Qiang Xue, the creator of Yii, liked it so much that he linked to my series from the Yii's official documentation, giving the series a good amount of publicity and traffic. I have wanted to write this book for some time, but did not have the opportunity until 2012.

While a large percentage of my work is technical writing, I'm an active developer. Most of the Web sites I do are for educational and non-profit organizations, but I also consult on commercial and other projects. I would estimate that I use a framework on maybe 60% of the sites I work on---because a framework isn't always appropriate. Some of the framework-based sites I create use WordPress instead of Yii.

My Web site is LarryUllman.com. This book's specific set of pages is at yii.LarryUllman.com. You can also find me on Twitter @LarryUllman.

Getting Help

If you need assistance with your Yii-based site, or with any of this book's materials, there are many places to turn:

{NOTE} If you don't have an IRC client (or haven't used IRC before), the Yii Web site graciously provides a Web-based interface.

You should always start by looking at the Yii documentation when you need help. Over the course of the book, you'll learn how to use the docs to solve your own problems, most specifically the class reference.

If you're still having problems, the Yii forums are probably the best place to turn. They have an active and smart community. Do begin by searching the forums first, as it's likely your question has already been raised and answered.

Understand that wherever you turn to for assistance, you'll get far better results if you provide all the necessary information, are patient, and demonstrate appreciation for the help.

You can contact me directly with questions, but I would strongly prefer that you use my support forums or the Yii forums instead. By using a forum, other people can assist, meaning you'll get help faster. Furthermore, the assistance will be public, which will likely help others down the line.

If you ask me for help via Twitter, Facebook, or Google+, I'll request that you use my or the Yii forums. If you email me, I will reply, but it's highly likely that it will take two weeks for me to reply. And the reply may say you haven't provided enough information. And after providing an answer, or not, I'll recommend you use forums instead of contacting me directly. So you can contact me directly, but it's far, far better---for you---if you use one of the other resources. Don't get me wrong: I want to help, but I want to help in the public forums.