Yii 2.0: yii2-app-practical

Yii 2 Practical Application Template
34 followers

Yii 2 Practical Application Template is a skeleton Yii 2 application based on the yii2-advanced template best for developing complex Web applications with multiple tiers. The template allows a practical method to directly access the frontend from the app root.

The template includes three tiers: front end, back end, and console, each of which is a separate Yii application.

The template is designed to work in a team development environment. It supports deploying the application in different environments.

Why yii2-practical?

After installing a app, in the yii2-advanced application you normally would access the frontend by:

http://domain/app/frontend/web

However, in many practical scenarios (especially on shared hosts) one would want their users to directly access frontend as:

http://domain/app

The yii2-app-practical enables you to achieve just that by carefully moving and rearranging the bootstrap files and web components of frontend to work directly out of the app root. The frontend/web is entirely eliminated and one can directly access the application frontend this way:

http://domain/app

All other aspects of the app configuration remain the same as the yii2-advanced app. The common, backend and console will remain as is. The frontend config, assets, models, controllers, views, widgets and components, will still reside within the frontend directory. It is just the web access that is moved out to app root.

Note: This template offers a solution for developers running their app on a SHARED HOST or having complex needs to work with multiple subdomains without having any ability on their HOST to control the different webroots for different apps.

Some Key Additions

  1. The template has some security preconfigured for users with Apache web servers. It has a default .htaccess security configuration setup.
  2. The template has prettyUrl enabled by default and the changes have been made to .htaccess as well as urlManager component config in the common config directory.

Directory structure

ROOT
    /                   contains the frontend entry script and web resources
    /assets             contains the frontend web assets
common
    config/             contains shared configurations
    mail/               contains view files for e-mails
    models/             contains model classes used in both backend and frontend
    tests/              contains various tests for objects that are common among applications
console
    config/             contains console configurations
    controllers/        contains console controllers (commands)
    migrations/         contains database migrations
    models/             contains console-specific model classes
    runtime/            contains files generated during runtime
    tests/              contains various tests for the console application
backend
    assets/             contains application assets such as JavaScript and CSS
    config/             contains backend configurations
    controllers/        contains Web controller classes
    models/             contains backend-specific model classes
    runtime/            contains files generated during runtime
    tests/              contains various tests for the backend application
    views/              contains view files for the Web application
    web/                contains the entry script and Web resources
frontend
    assets/             contains application assets such as JavaScript and CSS
    config/             contains frontend configurations
    controllers/        contains Web controller classes
    models/             contains frontend-specific model classes
    runtime/            contains files generated during runtime
    tests/              contains various tests for the frontend application
    views/              contains view files for the Web application
vendor/                 contains dependent 3rd-party packages
environments/           contains environment-based overrides

Requirements

The minimum requirement by this application template is that your Web server supports PHP 5.4.0.

Installation

Install from an Archive File

Extract the archive file downloaded from GitHub to a directory named practical or your app name, that is directly under the Web root.

Note: When using a archive file method, the vendor folder is not automatically created. You must extract the yii2-advanced vendor folder from here. Then you must copy this folder directly under the app root (i.e. practical directory).

After this is complete, follow the instructions given in "GETTING STARTED".

Install via Composer

The preferred way to install this application template is through composer. If you do not have Composer, you may install it by following the instructions at getcomposer.org.

You can then install the application using the following command:

php composer.phar create-project --prefer-dist --stability=dev kartik-v/yii2-app-practical practical

Getting Started

After you install the application, you have to conduct the following steps to initialize the installed application. You only need to do these once for all.

  1. Run command init to initialize the application with a specific environment.
  2. Create a new database and adjust the components['db'] configuration in common/config/main-local.php accordingly.
  3. Apply migrations with console command yii migrate. This will create tables needed for the application to work.
  4. Set document roots of your Web server:
  • for frontend /path/to/yii-application/ and using the URL http://frontend/
  • for backend /path/to/yii-application/backend/web/ and using the URL http://backend/

Frontend Access: Just navigate to http://yourdomain/practical (where practical is your app name folder under web root).

To login into the application, you need to first sign up, with any of your email address, username and password. Then, you can login into the application with same email address and password at any time.

Testing

Install additional composer packages: * php composer.phar require --dev "codeception/codeception: 1.8.*@dev" "codeception/specify: *" "codeception/verify: *"

This application boilerplate use database in testing, so you should create three databases that are used in tests: * yii2_practical_unit - database for unit tests; * yii2_practical_functional - database for functional tests; * yii2_practical_acceptance - database for acceptance tests.

To make your database up to date, you can run in needed test folder yii migrate, for example if you are starting from frontend tests then you should run yii migrate in each suite folder acceptance, functional, unit it will upgrade your database to the last state according migrations.

To be able to run acceptance tests you need a running webserver. For this you can use the php builtin server and run it in the directory where your main project folder is located. For example if your application is located in /www/practical all you need to is: cd /www and then php -S 127.0.0.1:8080 because the default configuration of acceptance tests expects the url of the application to be /practical/. If you already have a server configured or your application is not located in a folder called practical, you may need to adjust the TEST_ENTRY_URL in frontend/tests/_bootstrap.php and backend/tests/_bootstrap.php.

After that is done you should be able to run your tests, for example to run frontend tests do:

  • cd frontend
  • ../vendor/bin/codecept build
  • ../vendor/bin/codecept run

In similar way you can run tests for other application tiers - backend, console, common.

You also can adjust you application suite configs and _bootstrap.php settings to use other urls and files, as it is can be done in yii2-basic.

Documentation

You can view the documentation and submit your comments.

Report

License

yii2-app-practical is released under the BSD 3-Clause License. See the bundled LICENSE.md for details.

Resources

Total 5 comments

#18410 report it
Kartik V at 2014/10/25 04:28am
Re: Is it updated with released ver of Yii?

Yes, its updated to the last stable released version of yii in Oct 2014.

#18405 report it
Mariaczi+PL at 2014/10/24 03:28pm
IS it updatd with RELEASED VER of Yii?

IS it updatd with RELEASED VER of Yii?

#17217 report it
Kartik V at 2014/05/13 02:46pm
Some Key Additions
  1. The template has some security preconfigured for users with Apache web servers. It has a default .htaccess security configuration setup.
  2. The template has prettyUrl enabled by default and the changes have been made to .htaccess as well as urlManager component config in the common config directory.
#17047 report it
Kartik V at 2014/04/28 01:25pm
Re: URL rewrite

Yes, it depends on your environment and what you want to achieve. This is just a app template tweaking some of the yii basic config files (does not magically add anything else).

If you do have a host where you have accesses to use something like VHost then you may not need this - but the good thing is it will work even there immediately after app install.

It is a saving solution for developers who are restricted to shared hosts or do not have superuser access OR methods to do any URL rewrite and such.

The other important use case on shared hosts - is that if you have one domain and want to release various apps using subdomains... (like I do in my case)

  • http://www.krajee.com,
  • http://demos.krajee.com
  • http://plugins.krajee.com

... each of the above are on a single shared host... I cannot do much with it other than using such an option with the yii2-app-practical settings as my hosting provider does not allow me to control such options within (other than setting a webroot for a domain).

Note: The objective of this yii2-app-practical template was to have an app install option for Yii which is exactly similar to yii2-advanced-app - but offers the frontend access from the webroot by default after install.

#17046 report it
robregonm at 2014/04/28 12:56pm
An URL rewrite or VHost can solve this

After taking a look at this, I realized that despite of your proposal looks good and interesting, the main issue you are trying to solve s not a real issue, but a feature, since you can solve the Url routejust using a Virtual Host or adding an alias to the app. So this solution/alternative might be useful, in a shared hosting (where you cannot configure this stuff). Regards.

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet