Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter 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
€18.99 per month
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
€8.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
€18.99 per month
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
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781785882425
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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
€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 By Example
€36.99
Mastering Yii
€41.99
Yii2 Application Development Cookbook
€41.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.