Yii Framework 2 (Yii2) is the successor to the popular Yii framework. Like its successor, Yii2 is an open source, high-performance rapid development framework designed to create modern, scalable, and performant web applications and APIs.
Designed for both developers with no exposure to Yii and Yii2 and for Yii framework developers looking to become experts with Yii2, this book will serve as your guide to becoming a master of Yii. From initialization and configuration to debugging and deployment, this book will be your guide to becoming a master of all aspects of this powerful framework.
Chapter 1, Composer, Configuration, Classes, and Path Aliases, covers the basics of a Yii2 application. In this chapter, you'll learn the core conventions of Yii2 and how to configure it as a multi-environment application. You'll also discover how to use Composer, a dependency management tool for managing your applications' software dependencies.
Chapter 2, Console Commands and Applications, focuses on how to use the built-in Yii2 console commands as it guides you through creating your own commands.
Chapter 3, Migrations, DAO, and Query Building, teaches you how to create migrations in Yii2 and how to interact with your database using database access objects (DAO) and how to use Yii2's query builder.
Chapter 4, Active Record, Models, and Forms, teaches you how to create and use Active Record to effortlessly interact with a database. Furthermore, you'll also discover how to create models to represent information not stored in databases and how to create web forms based upon Active Record models and normal models.
Chapter 5, Modules, Widgets, and Helpers, covers how to incorporate modules inside of our application. This chapter will also cover how to create and use dynamic widgets and will additionally cover Yii2's powerful helper classes.
Chapter 6, Asset Management, focuses on how to create and manage our assets using asset bundles and how to manage our assets using the asset command. This chapter also covers several strategies to build and generate our asset library using powerful tools such as Node Package Manage and Bower.
Chapter 7, Authenticating and Authorizing Users, teaches you how to verify the authenticity of users in Yii2 using several common authentication schemes (such as OAuth authentication, basic HTTP authentication, and header authentication) as well as shows you how to grant them access to specific sections of your applications.
Chapter 8, Routing, Responses, and Events, focuses on how Yii2's routing and response classes work in Yii2. In this chapter, we'll cover how to handle data both in and out of our application and discover how to tap into Yii2's powerful event system.
Chapter 9, RESTful APIs, talks about how to quickly and effortlessly extend your application with a RESTful JSON and XML API using Yii2's ActiveController class.
Chapter 10, Testing with Codeception, helps you learn how to create unit, functional, and acceptance tests for your applications using a powerful testing tool called Codeception. In this chapter, you'll also learn how to create fixtures to represent your data for testing purposes.
Chapter 11, Internationalization and Localization, covers how to localize our applications and build them to support multiple languages. Additionally, you will master how to create and manage translation files using Yii2 console commands.
Chapter 12, Performance and Security, covers many ways to improve the performance of your Yii2 application and how to keep it secure against modern day attacks on web applications.
Chapter 13, Debugging and Deploying, helps you become well-versed in how to debug your Yii2 applications using both application logging and the Yii2 debug tool. Furthermore, you will discover the fundamentals of deploying your Yii2 applications in a seamless and non-disruptive fashion.
To ensure a consistent development environment and prevent unnecessary alterations to your host operation system, it is highly recommended that you run all commands within a Linux virtual machine. This will ensure that your output both in your web browser and from your command line matches the output that is presented in this book. As setting up this environment on your own can be a daunting task, prebuilt virtual machines that use VirtualBox and Vagrant are provided to make this setup process easy.
To get started with this book, you should be running the latest version of either Microsoft Windows 7, 8, 8.1 or 10, Apple OS X 10.9 or higher, or a Linux operating system that can run virtual machines, such as Ubuntu 14.04 LTS. Additionally, you will need to install the latest version of VirtualBox (available at https://www.virtualbox.org/wiki/Downloads) and Vagrant (available at https://www.vagrantup.com/downloads.html).
Note
After installing these software dependencies, you may need to restart your computer for the changes to take effect.
After installing VirtualBox and Vagrant, you can then create a new dedicated development environment by opening a new command line or terminal window, creating a new directory for the chapter, and then running the following command to create your virtual machine development environment. These commands will download a prebuilt virtual machine containing all the software required to get you started and start your new development environment:
vagrant init charlesportwoodii/php56_trusty64 vagrant up --provider virtualbox vagrant ssh
Note
More information on this specific Vagrant box can be found at https://atlas.hashicorp.com/charlesportwoodii/boxes/php56_trusty64.
Note that if you are on Windows, you may need a tool such as PuTTy to connect to your virtual machine over SSH. More information on how to connect to your new virtual machine over SSH on Windows can be found at http://docs-v1.vagrantup.com/v1/docs/getting-started/ssh.html.
Once your new Vagrant box has started, you can access the files of this virtual machine over SSH and access your webroot
directory by opening a new browser window and navigating to http://localhost:8080
. By default, when you open this web page, you will see the output of phpino()
.
Tip
Depending upon your operating system security settings, your computer may prompt or block you from accessing port 8080 on your computer. Ensure that you configure your firewall settings if you are facing issues and ensure that port 8080 is open on your computer and that VirtualBox can forward connections from your host operating system to your guest operating system.
As Yii2 is fully compatible with PHP7, it is strongly suggested that you develop and test your web applications against PHP7 as well. The following commands will allow you to provision a PHP7 Vagrant box:
vagrant init charlesportwoodii/php7_trusty64 vagrant up --provider virtualbox vagrant ssh
Tip
As these virtual machines automatically configure port forwarding, it is recommended that you only run a single virtual machine at a time. Refer to the Vagrant documentation for a complete list of commands and configuration options at https://docs.vagrantup.com/v2.
Mastering Yii is for intermediate to experienced software developers who want to quickly master Yii2. This book assumes some familiarity with PHP 5, HTML5, and rudimentary software development practices and methodologies.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: " This script tells Composer that when the create-project
command is run, it should run the postCreateProject
static function."
A block of code is set as follows:
"scripts": { "post-create-project-cmd": [ "yii\\composer\\Installer::postCreateProject" ] }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
// Define our application_env variable as provided by nginx/apache if (!defined('APPLICATION_ENV')) { if (getenv('APPLICATION_ENV') != false) define('APPLICATION_ENV', getenv('APPLICATION_ENV')); else define('APPLICATION_ENV', 'prod'); } $env = require(__DIR__ . '/config/env.php');
Any command-line input or output is written as follows:
$ ./yii fixture/load <FixtureName> $ ./yii fixture/unload <FixtureName>
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: " Once we have specified all the necessary attributes, we can click on the Preview button to preview our form, and then we can click on the Generate button to generate the source code."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>
, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
The latest and most up to date copies of source code for this book is maintained on the Packt website: http://www.packtpub.com and on GitHub at https://github.com/masteringyii, for each chapter where applicable.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>
, and we will do our best to address the problem.