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
Arrow up icon
GO TO TOP
Yii2 By Example

You're reading from   Yii2 By Example Develop complete web applications from scratch through practical examples and tips for beginners and more advanced users

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781785287411
Length 344 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Fabrizio Caldarelli Fabrizio Caldarelli
Author Profile Icon Fabrizio Caldarelli
Fabrizio Caldarelli
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Starting with Yii2 FREE CHAPTER 2. Creating a Simple News Reader 3. Making Pretty URLs 4. Creating a Room through Forms 5. Developing a Reservation System 6. Using a Grid for Data and Relations 7. Working on the User Interface 8. Log in to the App 9. Frontend to Display Rooms to Everyone 10. Localize the App 11. Creating an API for Use in a Mobile App 12. Create a Console Application to Automate the Periodic Task 13. Final Refactoring 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.

You have been reading a chapter from
Yii2 By Example
Published in: Sep 2015
Publisher:
ISBN-13: 9781785287411
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image