Book Image

Yii2 By Example

By : Fabrizio Caldarelli
Book Image

Yii2 By Example

By: Fabrizio Caldarelli

Overview of this book

Yii is a high-performance PHP framework best for developing Web 2.0 applications. It provides fast, secure, and professional features to create robust projects, however, this rapid development requires the ability to organize common tasks together to build a complete application. It's all too easy to get confused; this is where this book comes in. This book contains a series of practical project examples for developers starting from scratch. Each section contains the most relevant theories for every topic as you walk through developing each project, focusing on key aspects that commonly confuse users. The book starts with all the framework’s basic concepts, such as controllers and views, to introduce you to Yii and creating your first application, a simple news reader. You will be learn to configure URL rules to make a pretty URL, essential for search engine optimization. Next, you will walk through Model and ActiveRecord, key concepts in database interaction. The second application you will develop is a reservation system that allows you to manage rooms, customers, and reservations. For this, you will use database connection through SQL and ActiveRecord. More complex than the first one, this application will introduce you to the advanced template of Yii 2, splitting the app into two parts: a frontend for all visitors and a backend for the admin. Finally, you will move on to the last two applications: one that allows connections from remote clients, through RESTful components of Yii 2, and another that creates and organizes automatic tasks using the console application structure of Yii 2.
Table of Contents (20 chapters)
Yii2 By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Application structure


Yii2's application structure is very clear, precise, and redundant (for advanced applications).

The contents of the basic folder should be as follows:

Folder names

Description

assets

This includes the files (.js and .css) referenced in the web page and dependencies of the app.

commands

This includes the controllers used from the command line.

config

This includes the controllers used from web.

mail

This is the mail layout repository.

models

This includes the models used in the whole application.

runtime

This is used from Yii2 to store runtime data as logs.

tests

This includes all the test's repositories (unit, functional, fixtures, and so on).

vendor

This includes the third-party module repositories managed by Composer.

views

This contains PHP files, divided into folders that refer to controller names, used to render the main content of the page template. It is mainly called from the controller's actions to render the display output. A folder named layout contains the page template's PHP files.

web

This is the entry point from web

Open web/index.php to view content:

<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/../config/web.php');

(new yii\web\Application($config))->run();

Here, the first two constant definitions are very important.

YII_DEBUG defines whether you are in debug mode or not. If we set this, we will have more log information and will see the detail error call stack.

YII_ENV defines the environment mode we are working in, and its default value is prod. The available values are test, dev, and prod. These values are used in configuration files to define, for example, a different DB connection (local database different from remote database) or other values, always in configuration files.

Since we are at the start of our project, it is recommended to set YII_DEBUG to true, in order to have more detailed information in case we make a mistake in our code, instead of the unhelpful, blank.

The following table contains a list of all Yii2's objects:

Objects

Description

Models, Views, and Controllers

These are the common objects to apply the MVC pattern to:

  • Models are data representation and manipulation, usually from the database

  • Views are used to present data to the end user

  • Controllers are objects that process requests and generate responses

Components

These are objects that contain logic. The user can write his own components to create reusable functionalities.

For example, a component could be a currency converter object, which can be used at many instances in our application.

Application Components

They are singletons that can be called at any point in the app. Singleton means an object instanced just one time in the entire application (so the object will always be the same).

The difference between Application Components and Components is that the first can have just one instance in the whole application.

Widgets

These view reusable objects, containing both logic and rendering code. A widget could be, for example, a box displaying today's weather info.

Filters

These are objects that run before or after the execution of Controller actions. A filter can be used to change the format response output of the page, for example, from HTML to JSON.

Modules

This contains all the objects of an app, such as Models, Views, Controller, Components, and so on; we can consider them as subapp, containing reusable sections (for example, user management).

Extensions

Extensions are modules packaged, that we can easily manage using Composer.