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
NZ$80.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8 (5 Ratings)
Paperback Jan 2016 380 pages 1st Edition
eBook
NZ$44.99 NZ$64.99
Paperback
NZ$80.99
Subscription
Free Trial
Arrow left icon
Profile Icon Charles R. Portwood ll
Arrow right icon
NZ$80.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8 (5 Ratings)
Paperback Jan 2016 380 pages 1st Edition
eBook
NZ$44.99 NZ$64.99
Paperback
NZ$80.99
Subscription
Free Trial
eBook
NZ$44.99 NZ$64.99
Paperback
NZ$80.99
Subscription
Free Trial

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to New Zealand

Standard delivery 10 - 13 business days

NZ$20.95

Premium delivery 5 - 8 business days

NZ$74.95
(Includes tracking information)

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 : 9781785882425
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to New Zealand

Standard delivery 10 - 13 business days

NZ$20.95

Premium delivery 5 - 8 business days

NZ$74.95
(Includes tracking information)

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.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
$199.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 NZ$7 each
Feature tick icon Exclusive print discounts
$279.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 NZ$7 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total NZ$ 233.97
Yii2 Application Development Cookbook
NZ$80.99
Mastering Yii
NZ$80.99
Yii2 By Example
NZ$71.99
Total NZ$ 233.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

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact [email protected] with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at [email protected] using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on [email protected] with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on [email protected] within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on [email protected] who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on [email protected] within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela