Yii 2.0: yii2-app-practical-a

Yii 2 Practical-A Application Template
35 followers

Yii 2 Practical-A Application Template is a skeleton Yii 2 application based on the yii2-app-practical template, which in turn is based on the yii2-advanced template.

Since its based on the yii2-advanced template it is suitable for developing complex Web applications with multiple tiers. The template allows a practical method to directly access the frontend from the approot and backend from approot/backend.

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-app-practical-a?

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

http://domain/app/frontend/web
http://domain/app/backend/web

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

http://domain/app
http://domain/app/backend

The yii2-app-practical-a 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 and backend out of the backend. The frontend/web and backend/web folders are entirely eliminated and one can directly access the application frontend this way:

http://domain/app

and backend this way

http://domain/backend

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

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
    /                   contains the backend entry script and web resources
    assets/             contains backend runtime assets
    assets_b/           contains backend 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
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

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-a 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-a practical-a

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-a (where practical-a is your app name folder under web root).

Backend Access: Just navigate to http://yourdomain/practical-a/backend (where practical-a 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-a_unit - database for unit tests; * yii2_practical-a_functional - database for functional tests; * yii2_practical-a_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-a is released under the BSD 3-Clause License. See the bundled LICENSE.md for details.

Resources

Total 19 comments

#17685 report it
JFReyes at 2014/07/14 03:35pm
Also getting "Unable to resolve the request backend/site/login

I'm using Windows Server and IIS 7.5 with the URL Rewrite add-on installed. The frontend works fine except that the debug toolbar is not displayed in dev mode. Please help.

Thanks,

José

#17641 report it
Kartik V at 2014/07/10 12:59am
Purpose

@ThePr0f3550r - this is an app template (not exactly a real yii extension - but it is a package installed via composer). It is intended for new app installation and entirely depends on the choice of the developer and his environment. It can benefit those who want to install their app on shared hosts and do not have access to methods like VHost or URL rewrite on their host. It offers an alternative choice for people, who find it difficult configuring with the default yii2 advanced app.

As to the purpose of yii2-app-practical-a, its clearly highlighted in this article (Why section) or you can read about it here.

You may want to do some reading on app templates in Yii 2 and how they can be used by developers, if you are not clear.

#17640 report it
ThePr0f3550r at 2014/07/09 09:03pm
For what purpose?

Kartik .. I like Your extensions in another project but this is confusing, for what purpose?

I prefer You make boilerplate.. that contain Your extension.. and GII extended for Your extension..

Thx

#17535 report it
sn4k3 at 2014/06/29 04:37pm
Re: Extension Updated to v1.1.0 release

Thanks :)

#17534 report it
Kartik V at 2014/06/29 04:13pm
Extension Updated to v1.1.0 release

Extension has been upgraded to v1.1.0 (refer CHANGE LOG). This is based on the latest yii2-advanced-app until 30-Jun-2014.

You should now not face the errors and also get in all the new updates to the yii2 core advanced app.

#17533 report it
sn4k3 at 2014/06/29 03:19pm
Re: Unable to resolve the request "backend/site/login".

I have fixed... I copy .htaccess from the root to backend and it works...

Also layout files should be updated with: <?= Html::csrfMetaTags() ?> And also Security class dont exists any more, now we calll: \Yii::$app->getSecurity()

Thanks, great bootstrap

#17532 report it
Kartik V at 2014/06/29 02:59pm
Re: Unable to resolve the request "backend/site/login".

It seems there is some change to the yii core. Will do some tests and update.

#17531 report it
sn4k3 at 2014/06/29 02:37pm
Unable to resolve the request "backend/site/login".

I get the same problem...

Unable to resolve the request "backend/site/login".
#17264 report it
zvik2004 at 2014/05/17 03:51pm
RE: Configuration issue?

Same, I get:

Unable to resolve the request "backend/site/login". 
#17263 report it
Kartik V at 2014/05/17 02:05pm
Remove the second alias

You can remove the second alias /project_tools/backend from your conf file and let know.

#17262 report it
zvik2004 at 2014/05/17 01:30pm
RE: Configuration issue?

It solved the second problem.
Still, I don't have a way to get to the backend.
When I try to open 127.0.0.1/project_tools/backend/, I get the error:

Unable to resolve the request "backend/site/login". 

Thanks for your fast response,
Zvi.

#17261 report it
Kartik V at 2014/05/17 01:17pm
Check your rewrite config on Apache

@zvik2004 Seems like some issue for the urlmanager in understanding your base folder. You can try setting RewriteBase on your apache config. You can set this for example in your .htaccess file in the app root:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
  RewriteBase /project_tools/
  IndexIgnore */*
</IfModule>
#17259 report it
zvik2004 at 2014/05/17 01:09pm
Configuration issue?

Hi Kartik,
Thank you for all your work!
I've installed this (where my root directory is: c:/www/project_tools/src) but faced the following:

There was a warning in the apache log file:

The Alias directive in C:/Program Files (x86)/wamp/alias/project_tools_www.conf at line 2 will probably never match because it overlaps an earlier Alias.

The file: C:/Program Files (x86)/wamp/alias/project_tools_www.conf contains:

Alias /project_tools "c:/www/project_tools/src" 
Alias /project_tools/backend "c:/www/project_tools/src/backend"

Another thing,
When I opened http://127.0.0.1/project_tools, I have no problem to see the home page, but when I try to press on one of the links (e.g. About), I got the following error:

Not Found

The requested URL /www/project_tools/src/index.php was not found on this server.

(seems like C:/ is removed from the path)

Thanks,
Zvi.

#17229 report it
Richard M at 2014/05/14 09:20am
Very useful template

This is a very useful template mate. Keep it up. Badly needed something like this for my domain.

#17228 report it
Kartik V at 2014/05/14 09:13am
Question to Yii Mod

Let know where app templates from vendors need to be posted (under extensions or wiki or elsewhere)?

#17227 report it
Kartik V at 2014/05/14 08:56am
Extended App Template

This is a yii2 app template which needs to be installed using composer like any extension. This template is a custom template and has its own repository like any vendor extension.

#17226 report it
Rajith R at 2014/05/14 05:43am
Extension?

what is this? write this under wiki.

#17182 report it
Kartik V at 2014/05/10 03:41am
Good point

@mmx nice points - could be collated for people who have similar restrictions on their server host.

#17181 report it
mmx at 2014/05/10 02:50am
Good solution for subdomains

Been using something similar since 1.x. For 2.x, you can add subdomain applications in the same fashion as backend, allowing all subdomain traffic to route through the frontend's bootstrap loader by parameterizing route names as suggested in the url.md part of the definitive guide. It's a much better solution than the Advanced template for those of us running subdomains with the same session and cookies.

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet