Home | About | Web Stories | View All Posts

15 Apr 2022

Do You Know About Yii2 Framework Complete Folder Structure

Yii2 framework uses and stratifies its application structure into different subdirectories and files in the root of the application.



Yii2 folder structure

Yii2 framework uses following subdirectories as the top level directories in the root of the application -
  1. backend- it is used to manage the whole application system as site administrators
  2. frontend- it is used to show the main interfaces to end users.
  3. console- it consists of the console commands needed by the application system.
  4. common- whose content or files are shared among the above applications i.e. files common to all applications.
  5. environments- represents environment configs eg. development or production mode related.
  6. vendor- represents place for third party modules and Yii2 source files itself.

Yii2 framework root directory uses a set of files as below -

  1. .gitignore- contains a list of directories ignored by git version system. If you need something never get to your source code repository, add it there.
  2. composer.json - Composer config described in Configuring Composer.
  3. init - initialization script described in Configuration and environments.
  4. init.bat - same for Windows.
  5. LICENSE.md - license info. Put your project license there. Especially when opensourcing.
  6. README.md - basic info about installing template. Consider replacing it with information about your project and its installation.
  7. requirements.php - Yii requirements checker.
  8. yii - console application bootstrap.
  9. yii.bat - same for Windows.

The frontend and backend both have these directories -

  1. components- contains components (e.g. helpers, widgets) that are only used by this application
  2. config- contains the configuration used by the application
  3. controllers- contains controller classes
  4. lib- contains third-party libraries that are only used by this application
  5. models- contains model classes that are specific for the application
  6. runtime- stores dynamically generated files
  7. views- stores controller actions view scripts
  8. web- the web root for this application

Yii2 advanced template

There are three applications in the advanced template -

  1. frontend - It is usually what is presented to the end user, the project itself.
  2. backend - It is an administration panel, analytics and similar functions.
  3. console - The console is typically used for cron jobs and low-level server administration. It is also used when deploying applications and handles migrations and assets. The directory structure for the console is slightly different because it doesn't need controllers, views, and www. Instead, it contains a command directory to store all console command class files.

Frontend and backend are web applications and both contain a web directory. This is the webroot you should point your webserver to.

There is also a common directory that contains files used by more than one application like, a user model. A common directory contains files that are shared between applications. For example, each application may need to access a database using ActiveRecord. Therefore, Yii can store the AR model classes in a common directory. Similarly, if some helper or widget classes are used in more than one application, Yii also file them under common to avoid code duplication. To make code easier to maintain, Yii organizes a common directory into a structure similar to that of an application. For example, it has components, models, library, etc.

Because applications can also share some configurations. Therefore, Yii also stores common configurations in the config directory in the common folder. Yii also uses the DB migration feature to track database changes for large projects with a long development cycle. DB saves all migrations in the migrations directory in a common folder.

The following image represents the full directory structure for Yii2 advance template "Yii2 advanced structure" -

Yii2 Framework Complete Folder Structure

Predefined path aliases for accessing the Yii directory structure

Yii uses the aliases specific to the directory structure of the advanced application. The following are the list of few aliases -

  • @yii - framework directory.
  • @app - base path of currently running application.
  • @common - common directory.
  • @frontend - frontend web application directory.
  • @backend - backend web application directory.
  • @console - console directory.
  • @runtime - runtime directory of currently running web application.
  • @vendor - Composer vendor directory.
  • @bower - vendor directory that contains the bower packages.
  • @npm - vendor directory that contains npm packages.
  • @web - base URL of currently running web application.
  • @webroot - web root directory of currently running web application.

You can get the path of a 'path alias' using the codes at any view file in frontend or backend using the codes as like - "echo Yii::getalias('@app');", "echo Yii::getalias('@web');". The following are the list of aliases with uses code sample -

  • @app: Your application root directory (either frontend or backend or console depending on where you access it from)
      <?php  echo Yii::getalias('@app'); ?>
      //It will produce - C:\xampp\htdocs\yii2projectname\frontend
  • @vendor: Your vendor directory on your root app install directory
      <?php  echo Yii::getalias('@vendor'); ?>
      //It will produce - C:\xampp\htdocs\yii2projectname\vendor
  • @runtime: Your application files runtime/cache storage folder
      <?php  echo Yii::getalias('@runtime'); ?>
      //It will produce - C:\xampp\htdocs\yii2projectname\frontend\runtime
  • @web: Your application base url path
      <?php  echo Yii::getalias('@web'); ?>
      //It will produce - /yii2projectname/frontend/web
  • @webroot: Your application web root
      <?php  echo Yii::getalias('@webroot'); ?>
      //It will produce - C:/xampp/htdocs/yii2projectname/frontend/web
  • @common: Alias for your common root folder on your root app install directory
      <?php  echo Yii::getalias('@common'); ?>
      //It will produce - C:\xampp\htdocs\yii2projectname\common
  • @frontend: Alias for your frontend root folder on your root app install directory
      <?php  echo Yii::getalias('@frontend'); ?>
      //It will produce - C:\xampp\htdocs\yii2projectname\frontend
  • @backend: Alias for your backend root folder on your root app install directory
      <?php  echo Yii::getalias('@backend'); ?>
      //It will produce - C:\xampp\htdocs\yii2projectname\backend
  • @console: Alias for your console root folder on your root app install directory
      <?php  echo Yii::getalias('@console'); ?>
      //It will produce - C:\xampp\htdocs\yii2projectname\console
  • @yii, the directory where the BaseYii.php file is located (also called the framework directory).
      <?php  echo Yii::getalias('@yii'); ?>
      //It will produce - C:\xampp\htdocs\yii2cms2022\vendor\yiisoft\yii2
  • @bower, the root directory that contains bower packages. Defaults to @vendor/bower.
      <?php  echo Yii::getalias('@bower'); ?>
      //It will produce - C:\xampp\htdocs\yii2cms2022/vendor/bower-asset
  • @npm, the root directory that contains npm packages. Defaults to @vendor/npm.
      <?php  echo Yii::getalias('@npm'); ?>
      //It will produce - C:\xampp\htdocs\yii2cms2022/vendor/npm-asset

Yii application structure overview

Yii applications are organized according to the model-view-controller (MVC) architectural design pattern as explained below -

  • Models - It represents data, business logic, and rules
  • Views - These are output representations of models
  • Controllers - It takes input and translate it into commands for models and views

In addition to MVC, Yii applications also have the following entities -

  • entry scripts: are PHP scripts that are directly accessible to end users. They are responsible for initiating the request processing cycle.
  • application: these are globally accessible objects that manage application components and coordinate them to fulfill requests.
  • application components: they are objects registered with applications and provide various services to fulfill requests. For example - yii\swiftmailer\Mailer: supports mail composing and sending, session - represents the session information, urlManager, user - represents the user authentication information, view - supports view rendering, etc.
  • modules: these are separate packages that contain complete MVC by themselves. The modules are often viewed as mini-applications. Modules differ from applications in that modules cannot be deployed alone and must reside within applications. For example - forum module. The following is the sample folder structure of the forum module -
      forum/
        Module.php                   the module class file
        controllers/                 containing controller class files
            DefaultController.php    the default controller class file
        models/                      containing model class files
        views/                       containing controller view and layout files
            layouts/                 containing layout view files
            default/                 containing view files for DefaultController
                index.php            the index view file
                
  • filters: represent the code that needs to be called before and after each request is actually processed by the controllers. Yii provides a set of commonly used filters, found primarily under the yii\filters namespace. For example - AccessControl, HttpBasicAuth, HttpCache, PageCache, VerbFilter, Cors, etc.
  • widgets: are objects that can be inserted into views. They can contain controller logic and can be reused in different views. For example - a date picker widget

FAQ - Frequently Asked Question


Is Yii a good framework?

Yes it is a good framework. It is a secure, fast and efficient widly used PHP framework.

Is yii2 an MVC?

Yes, Yii2 is a PHP framework based on the MVC design pattern. The MVC design pattern is used to separate business logic from user interface considerations so that developers can more easily change each part without affecting the other.

Is yii2 better than Laravel?

It depends on the project requirement that you want to use. While Laravel can be used for building a web application of any complexity and any type of business, Yii works better for creating web applications with high traffic flow, real-time applications and web applications which have admin panels or dashboards.


Best Books for Web Development in PHP


1

Programming PHP : Creating Dynamic Web Pages

Programming PHP : Creating Dynamic Web Pages

Book Description

It's the new and updated version of this book - Fourth Edition - that teaches you everything you need to know to build effective web applications using the latest features in PHP 7.4.

The book explains language syntax, programming techniques, and other details using examples that illustrate both correct usage and common idioms.

For those with a working knowledge of HTML, the book contains many style tips and practical programming advice in a clear and concise manner to help you become a top PHP programmer.

The book teaches about fundamentals of the language including data types, variables, operators and flow control statements. It explores about functions, strings, arrays and objects.

It teaches to apply common web application techniques, such as form processing, data validation, session tracking, and cookies.

It teaches to interact with relational databases such as MySQL or NoSQL databases such as MongoDB. It also teaches to generate dynamic images, creating PDF files, and parsing XML files.

You can learn about secure scripts, error handling, performance tuning and other advanced topics in this book.

You can get a quick reference to PHP core functions and standard extensions in this book.

Book details

Format: Kindle Edition, Paperback
Rating: 4.6 out of 5
Author: Kevin Tatroe, Peter Macintyre
Print Length: 540 pages
Publication Date: 27 March 2020
Publisher: O′Reilly, 4th edition
Kindle Price: Rs. 1,567.50*
Paperback Price: Rs. 3,614.00*
*Price and stock are correct and available at the time of article publication.

Get it here from Amazon


2

PHP Web Development with Laminas

PHP Web Development with Laminas

Book Description

This book teaches how to build fully secure and functional e-commerce applications with PHP using the next generation Zend Framework-Laminas. You can learn to develop modern object-oriented applications with PHP by using Test-Driven Development (TDD) and Behavior-Driven Development (BDD) aided by mature reusable components.

This book provides a practical approach to equip you with the knowledge of the Laminas framework needed to start building web applications based on reuse of loosely coupled components.

You will learn how to build the basic structure of a PHP web application divided into layers. You can understand the MVC components of Laminas and be able to take advantage of the Eclipse platform as a method to develop with Laminas.

Books teach to explore how object-relational mapping is implemented with Laminas-DB, behavior-driven development concepts to sharpen your skills, how to build complete models and reusable components, practice testing How to Create HTML Forms With Laminas-Forms.

By the end of this web development book, you will be able to build completely secure MVC applications in PHP language using Laminas.

Book details

Format: Kindle Edition
Author: Flávio Gomes da Silva Lisboa
Text-to-Speech: Enabled
Enhanced typesetting: Not Enabled
X-Ray: Not Enabled
Word Wise: Not Enabled
Publication Date: 9 December 2022
Publisher: Packt Publishing
Kindle Price: Rs. 750.74*
*Price and stock are correct and available at the time of article publication.

Get it here from Amazon


3

Getting started with Laravel 9, master the most popular PHP framework

Getting started with Laravel 9, master the most popular PHP framework

Book Description

This book is for all those who want to build their first application in Laravel 9. This book provides a step-by-step introduction to the writing framework, gets to know its most relevant aspects and focuses above all on practice.

Using this book you will be able to build any basic application with the framework. There are total 19 chapters in this book. Using this book, you will be able to know what are the required software to install Laravel for different operating systems.

In this book you can learn - project creation, database configuration, routing, view controllers, redirection, directive and templating engines in the form of blades, model building, CRUD applications etc.

You can learn to perform common eloquent operations that can be applied to databases using query builders. You can learn how to generate test data using classes.

You can also learn the file upload process. You can learn how to use REST APIs through CRUD type applications in VU3 using Axios requests and web components with Oruga UI.

You can also learn how to configure Browsersync with Laravel to automatically reload applications. You can learn how to protect an app in Vue with the login required to access its various modules using SPA authentication or Laravel Sanctum tokens.

Book details

Format: Kindle Edition
Rating: 1 out of 5
Author: Andrés Cruz Yoris
Print Length: 453 pages
Publication Date: 8 May 2022
Text-to-Speech: Enabled
Screen Reader: Supported
Enhanced typesetting: Enabled
X-Ray: Not Enabled
Word Wise: Not Enabled
Kindle Price: Rs. 449.00*
*Price and stock are correct and available at the time of article publication.

Get it here from Amazon


4

Learning Drupal as a framework: Your guide to custom Drupal 9. Full code included

Learning Drupal as a framework: Your guide to custom Drupal 9. Full code included

Book Description

This book uses PHP> 7.4. This course teaches you about the advanced concepts of Drupal 9, object-oriented PHP and Symfony components.

After the course, you will be able to build a variety of robust and scalable software solutions.

This book discusses advanced topics such as custom entities, entity forms, access controls, events, caching, workflows, and more when building real software.

It gives you powerful and ready-to-use snippets for your next Drupal project with +2400 lines of custom code.

Book details

Format: Kindle Edition
Rating: 5 out of 5
Author: Stef Van Looveren
Print Length: 282 pages
Publication Date: 17 July 2022
Text-to-Speech: Enabled
Screen Reader: Supported
Enhanced typesetting: Enabled
X-Ray: Not Enabled
Word Wise: Not Enabled
Kindle Price: Rs. 449.00*
*Price and stock are correct and available at the time of article publication.

Get it here from Amazon


5

Getting started with CodeIgniter 4

Getting started with CodeIgniter 4

Book Description

This book is for anyone who wants to build their first applications in CodeIgniter 4 a popular PHP framework, this writing offers a step-by-step introduction to the framework, knowing the most relevant aspects of it, and is focused above all on practice.

The book is aimed at those people who want to learn something new, learn about a framework that has very little documentation, who want to improve a skill in web development, who want to grow as a developer, and who want to continue scaling their path with other frameworks superior to this one.

This book has a total of 15 chapters and consists of explanations and practices. It teaches you how to run the framework, how to configure a database, how to create the first components, how to use of migrations for table management, working with the MVC, how to prepare CRUD application, how to use the routes, grouped routes, their options, and the different types.

You can learn about the use of the session and also of the flash session to save data and present it to the user. You can learn to manage views in a reusable way. You can learn about how to work with HTML forms and apply validations from the server side in CodeIgniter. You can learn about the authentication module with the login interface, and how to build a Rest Api type CRUD that can be consumed with JSON or XML.

You can also learn about generating test data with seeders, how to handle the relational schema of the database, how to do uploading files in the application, how to use libraries and help functions, how to integrate the PayPal platform, etc.

Book details

Format: Kindle Edition
Author: Andres Cruz
Print Length: 328 pages
Publication Date: 13 May 2022
Text-to-Speech: Enabled
Screen Reader: Supported
Enhanced typesetting: Enabled
X-Ray: Not Enabled
Word Wise: Not Enabled
Kindle Price: Rs. 319.00*
*Price and stock are correct and available at the time of article publication.

Get it here from Amazon


Tags :
Aashutosh Kumar Yadav

By Aashutosh Kumar Yadav

He is a PHP-based UI/Web designer and developer by profession and very interested in technical writing and blogging. He has been writing technical content for about 10 years and has proficient in practical knowledge and technical writing.
@www.infotokri.in

0 comments:

Post a Comment