Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Mastering Yii
Mastering Yii

Mastering Yii: Advance your modern web application development skills with Yii Framework 2

Arrow left icon
Profile Icon Charles R. Portwood ll
Arrow right icon
€22.99 €32.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8 (5 Ratings)
eBook Jan 2016 380 pages 1st Edition
eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Charles R. Portwood ll
Arrow right icon
€22.99 €32.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8 (5 Ratings)
eBook Jan 2016 380 pages 1st Edition
eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Mastering Yii

Chapter 2. Console Commands and Applications

Often when building modern web applications, we need to write background and maintenance tasks to support our main application. These tasks may include things such as generating reports, sending e-mails via a queuing system, or even running data analysis that would cause a web-based endpoint to timeout. With Yii2, we can build these tools and scripts directly into our application by writing console commands or even complete console applications.

Configuration and usage

The basic structure of Yii2 console applications is very similar to the structure used in web applications. In Yii2, console commands that extend from yii\console\Controller are nearly identical to yii\web\Controller.

Entry script

Before moving on to the configuration files themselves, let's take a look at the console entry script, which is part of the file called yii. This entry script serves as the bootstrapper for all our console commands, and in general, they can be run by calling this:

$ ./yii

This command will output all the currently available commands for the system. Like the web/index.php entry script, though, it isn't aware of its environment yet. We can change this by replacing yii with the following code block:

#!/usr/bin/env php
<?php
/**
 * Yii console bootstrap file.
 */

// Define our application_env variable as provided by nginx/apache
if (!defined('APPLICATION_ENV'))
{
    if (getenv('APPLICATION_ENV') != false)
  ...

Built-in console commands

Now that we know how to run console commands, let's take a look at the built-in commands to see how they work. As shown previously, Yii2 has seven built-in console commands: help, asset, cache, fixtures, gii, message, and migrate. During the development of our application, we're likely to use all seven in order to make our application more robust. Let's take a look at each one in more detail.

The help command

The first command built in to Yii2 is the help command. Often when running console commands, you may not know what options a certain command needs. Rather than referencing the Yii2 documentation, you can use the help command to provide you with all the core information you need.

At the most basic level, the help command will output all the currently available console commands:

$ ./yii help

Some commands contain additional subcommands that can be run. To view a list of all the available subcommands for a given command, you can run this:

$ ./yii help...

Creating console commands

Now that we know what built-in commands Yii2 provides, let's start adding our own commands. In Yii2, any custom commands we write are going to be stored in the /commands subfolder of our application. If this folder doesn't exist yet, go ahead and create it:

mkdir commands

Now, let's write a basic console command that just outputs some text:

  1. First, we'll create a new file called BasicController.php in the commands folder:
    touch commands/BasicController.php
    
  2. Now, let's write some PHP code. First, we need to declare the namespace that our BasicController lives in. This namespace directly corresponds to the controllerNamespace parameter we defined in config/console.php:
    <?php
    
    namespace app\commands;
  3. Then, we'll want to declare that we want to use the \yii\console\Controller class in our new controller:
    use \yii\console\Controller;
  4. Next, we'll declare our controller class as follows:
    class BasicController extends Controller { }
  5. Finally,...

Summary

In this chapter, we covered how to configure Yii to run console commands in a manner consistent with our web applications. We also covered the seven built-in console commands in brief. Additionally, we covered how to create our own console commands, how to pass parameters to our command, how to return values properly from within our code, and how to format the output of our commands.

In the next chapter, we'll expand our mastery of Yii by learning how to use and write migrations, how to use database access objects (DAO), and how to use Yii's built-in Query Builder.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn to work with the key aspects of Yii Framework 2
  • Explore how to create RESTful APIs with Yii
  • Incorporate codeception with Yii2 to test your code thoroughly

Description

The successor of Yii Framework 1.1, Yii 2 is a complete rewrite of Yii Framework, one of the most popular PHP 5 frameworks around for making modern web applications. The update embraces the best practices and protocols established with newer versions of PHP, while still maintaining the simple, fast, and extendable behavior found in its predecessor. This book has been written to enhance your skills and knowledge with Yii Framework 2. Starting with configuration and how to initialize new projects, you’ll learn how to configure, manage, and use every aspect of Yii2 from Gii, DAO, Query Builder, Active Record, and migrations, to asset manager. You'll also discover how to automatically test your code using codeception. With this book by your side, you’ll have all the skills you need to quickly create rich modern web and console applications with Yii 2.

Who is this book for?

This book is for Yii Framework developers who want to quickly master the advanced functions available in Yii 2. This book assumes some familiarity with Yii2, PHP 5, and HTML5.

What you will learn

  • Explore Yii2's conventions and learn how to properly configure Yii2
  • Create both web and console applications
  • Reduce development time by learning to create classes automatically with Gii, Yii2's automatic code generation tool
  • Use Yii2's database migration tool
  • Manage and access databases with Active Record, DAO, and Query Builder
  • Handle user authentication and authorization within Yii2
  • Create RESTful APIs with Yii Framework 2
  • Test applications automatically with codeception

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 28, 2016
Length: 380 pages
Edition : 1st
Language : English
ISBN-13 : 9781785888649
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jan 28, 2016
Length: 380 pages
Edition : 1st
Language : English
ISBN-13 : 9781785888649
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 120.97
Yii2 Application Development Cookbook
€41.99
Mastering Yii
€41.99
Yii2 By Example
€36.99
Total 120.97 Stars icon
Banner background image

Table of Contents

14 Chapters
1. Composer, Configuration, Classes, and Path Aliases Chevron down icon Chevron up icon
2. Console Commands and Applications Chevron down icon Chevron up icon
3. Migrations, DAO, and Query Building Chevron down icon Chevron up icon
4. Active Record, Models, and Forms Chevron down icon Chevron up icon
5. Modules, Widgets, and Helpers Chevron down icon Chevron up icon
6. Asset Management Chevron down icon Chevron up icon
7. Authenticating and Authorizing Users Chevron down icon Chevron up icon
8. Routing, Responses, and Events Chevron down icon Chevron up icon
9. RESTful APIs Chevron down icon Chevron up icon
10. Testing with Codeception Chevron down icon Chevron up icon
11. Internationalization and Localization Chevron down icon Chevron up icon
12. Performance and Security Chevron down icon Chevron up icon
13. Debugging and Deploying Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8
(5 Ratings)
5 star 80%
4 star 20%
3 star 0%
2 star 0%
1 star 0%
Gene Rodrigues Jan 04, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Avid Yii user since 1.x. Have had some odd experiences with PACKT books in the past caused by poor technical editing and error prone examples, but this book is a good read and well thought out.
Amazon Verified review Amazon
jazzsgood Apr 19, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Hi,I'm a software architect from Germany. There are not many books out there for Yii2. It (the framework) competes with Symfony and Laravel, also two of my favourites. In this book all relevant topics are covered, also the tricky ones like "authenticating and authorizing users" and "database handling with DAO" and REST, another hype out there. Also the real world is there with debugging, performance and security. To get an good overview I would recommend this book aside the online documentation itself.
Amazon Verified review Amazon
Trejder Feb 15, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I'm one of the reviewers of this book, but I'm writing this review purely as a customer and book reader. I'm a Yii developer with 5+ years of experience.I have read this book thoroughly and I must admit that this is an outstanding piece of work. It targets a little bit more experienced Yii2 developers and thus does not focus on complete basics. Instead, it provides detailed information on most important topics, that covers application development.Book consists of thirteen chapters and nearly 350 pages. These pages are full of important details, tips, tricks and a large number of useful examples.Chapters are ordered from configuration, through key application development, to additional tasks around application life-cycle.It includes:- introductory topics (composer configuration and console commands or applications),- basic application development (DAO, queries, data providers, AR, modules, widgets, assets and helpers),- request life-cycle (authentication, authorization, routing, responses and events),- advanced topics (RESTful applications, translation and internationalization),- application life-cycle (testing, debugging, fixing performance and security).Every aspect is discussed in details, supported by many examples and useful tips. Chapters are written in the way, that both beginners and advanced developers can benefit from them.I can strongly advice anyone, who is around Yii2 development, to purchase this book. Covered material is vast and written in a very well form. I would most certainly bought this book, just as a Yii2 developer, without any remarks to the fact, that I was reviewing it during writing process.
Amazon Verified review Amazon
Maciej Stencel Apr 02, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I have been searching for long time one big comprehensive knowledge well for Yii2. Ive tried vide tutorials and other books but this one gave me everything what i needed to start using this framework.This book will lead you from installation to finished product. You will read about all Yii2 "things" like console, Active Record, modules and widgets, authentication, internationalization and how to build RESTful application.What will be missing for your specific project you'll find yourself as you will be by now fluent Yii2 developer.Worth buying.
Amazon Verified review Amazon
Amazon Customer Sep 03, 2017
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I've found this book to be very informative.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.