yii2-app-advanced on single domain (Apache, Nginx)
Introduction ¶
It doesn't matter if you're using a basic or advanced Yii2 installation, create a directory in the root of your yii2 project called 'themes.' Now go download the Bootstrap 4 theme of your choice. Yii2 templates basically is a starting point to develop an app, not a pre-made cms that has default logins. In this case, if you have executed migrations as outlined in the tutorial Apply migrations with console command yii migrate.
If you develop rather complex applications with Yii2, you might already be familiar with yii2-app-advanced application template. Well, the template is awesome and provides high flexibility regarding code/configuration sharing among parts of an application.
What is not obvious for this template, is how to configure a web server so that both, frontend and backend will be accessible on the same domain, like following:
http://example.com/
- frontendhttp://example.com/backend/
- backend
Option 1: The Easy Way ¶
This way should work on any Unix-like OS (MacOSX, GNU/Linux, FreeBSD etc).
All you need to do is to set web server Document Root to frontend/web
and inside of it create a symlink to backend/web
.
Creating symlink for backend ¶
Apache ¶
DocumentRoot
directive is NOT AVAILABLE in .htaccess file context! You should edit you httpd.conf or virtual host config file. Or modify corresponding setting in the hosting configuration interface.
Also make sure that FollowSymlinks
option is active for a virtual host.
Example virtual host config:
Nginx ¶
For Nginx just use official recommended configuration and remember to set root
accordingly:
Option 2: The Hard Way ¶
Try this option only if the previous one does not work for you. Because any additional rewrite rules is a performance hit.
To achieve our goal, we need to slightly modify backend configuration, as well as to alter the web server's configuration.
Yii configuration ¶
We need to 'teach' the application components request
and urlManager
about our intended URL structure.
Modify file frontend/config/main.php
:
Modify file backend/config/main.php
:
Apache (.htaccess with mod_rewrite) ¶
Create a file .htaccess
in the project root directory (where composer.json
is):
Nginx ¶
Yii2 Advanced Tutorial
Here is Nginx config, built on top of the official recommended configuration:
When we first installed the Yii Framework, we found that we were able to create a basic project in one step. We downloaded the repository with composer, and a new skeleton application was created for us all in once shot. This basic application is fantastic for testing out some basic features of Yii. With this basic setup, we get a navigation menu, an about page, a working contact form, as well as a functional login form. Yii also provides an advanced application structure during install. This advanced layout template is for when you're ready to build a full blown web site that features a front end, back end, in addition to a console application. It's quite impressive, so let's see how to set it up.
Step 1. Create an advanced project with composer
C:wampwwwyii>composer create-project --prefer-dist yiisoft/yii2-app-advanced advanced
When we took this first step with the basic application, that is all you had to do. Run a composer command and voila, your application was ready for use. The advanced template is just a bit more involved, but its pretty easy. In another tutorial, we'll learn about the Yii Application Instance. Lets see what else we need to do.
Step 2. Change to the advanced folder and run the init command
C:wampwwwyii>
cd advanced
C:wampwwwyiiadvanced>init
Yii Application Initialization Tool v1.0
Which environment do you want the application to be initialized in?
[0] Development
[1] Production
Your choice [0-1, or 'q' to quit] 0
Initialize the application under ‘Development' environment? [yes|no] y
Start initialization …
generate backend/config/main-local.php
generate backend/config/params-local.php
generate backend/web/index-test.php
generate backend/web/index.php
generate common/config/main-local.php
generate common/config/params-local.php
generate console/config/main-local.php
generate console/config/params-local.php
generate frontend/config/main-local.php
generate frontend/config/params-local.php
generate frontend/web/index-test.php
generate frontend/web/index.php
generate yii
generate cookie validation key in backend/config/main-local.php
generate cookie validation key in frontend/config/main-local.php
chmod 0777 backend/runtime
chmod 0777 backend/web/assets
chmod 0777 frontend/runtime
chmod 0777 frontend/web/assets
chmod 0755 yii
… initialization completed.
The advanced application is almost complete. You can visit the various pages that are already created, however if you try to login to the site or create a user you will likely encounter an error like this.
SQLSTATE[HY000] [1049] Unknown database ‘yii2advanced'
?
Caused by: PDOException
SQLSTATE[HY000] [1049] Unknown database ‘yii2advanced'
This brings up to the next step, which is to create the database for the advanced application.
Step 3. Create the database
mysql>
create database yii2advanced;
Query OK, 1 row affected (0.01 sec)
common/config/main-local.php source
At this point there is now a database ready for use, but there are no tables. We won't get too far without tables. Thankfully, we have a migration already created for us, all we have to do is run it.
Step 4. Run the Yii Migration Tool
Yii2 Advanced Template
[0] Development
[1] Production
Your choice [0-1, or 'q' to quit] 0
Initialize the application under ‘Development' environment? [yes|no] y
Start initialization …
generate backend/config/main-local.php
generate backend/config/params-local.php
generate backend/web/index-test.php
generate backend/web/index.php
generate common/config/main-local.php
generate common/config/params-local.php
generate console/config/main-local.php
generate console/config/params-local.php
generate frontend/config/main-local.php
generate frontend/config/params-local.php
generate frontend/web/index-test.php
generate frontend/web/index.php
generate yii
generate cookie validation key in backend/config/main-local.php
generate cookie validation key in frontend/config/main-local.php
chmod 0777 backend/runtime
chmod 0777 backend/web/assets
chmod 0777 frontend/runtime
chmod 0777 frontend/web/assets
chmod 0755 yii
… initialization completed.
The advanced application is almost complete. You can visit the various pages that are already created, however if you try to login to the site or create a user you will likely encounter an error like this.
SQLSTATE[HY000] [1049] Unknown database ‘yii2advanced'
?
Caused by: PDOException
SQLSTATE[HY000] [1049] Unknown database ‘yii2advanced'
This brings up to the next step, which is to create the database for the advanced application.
Step 3. Create the database
mysql>
create database yii2advanced;
Query OK, 1 row affected (0.01 sec)
common/config/main-local.php source
At this point there is now a database ready for use, but there are no tables. We won't get too far without tables. Thankfully, we have a migration already created for us, all we have to do is run it.
Step 4. Run the Yii Migration Tool
Yii2 Advanced Template
C:wampwwwyiiadvanced>yii migrate
Yii Migration Tool (based on Yii v2.0.0)
Creating migration history table 'migration'…done.
Total 1 new migration to be applied:
m130524_201442_init
Apply the above migration? (yes|no) [no]:y
*** applying m130524_201442_init
> create table {{%user}} … done (time: 0.031s)
*** applied m130524_201442_init (time: 0.055s)
Migrated up successfully.
The advanced Yii template is now full functional. There are quite a few differences between this install and the basic install. The main difference is the fact that the advanced template has three entry points into the application. These would be the front end, back end, and console. In addition to this, there is a whole new directory structure you'll need to be familiar with.
Yii Advanced Template Directory Structure
common | |
config/ | contains shared configurations |
mail/ | contains view files for e-mails |
models/ | contains model classes used in both backend and frontend |
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 |
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 |
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 |
views/ | contains view files for the Web application |
web/ | contains the entry script and Web resources |
widgets/ | contains frontend widgets |
vendor/ | contains dependent 3rd-party packages |
environments/ | contains environment-based overrides |
tests | contains various tests for the advanced application |
codeception/ | contains tests developed with Codeception PHP Testing Framework |
Custom Domains
Yii2 Advanced Crud Tutorial
Finally, you can set up custom domains for both the font and backend of the advanced site. Just like we did with the basic install, we can edit the hosts file to include these lines.
127.0.0.1 yiiadvanced.com
127.0.0.1 admin.yiiadvanced.com
We then update the httpd-vhosts.conf to include the front and back ends.
ServerName yiiadvanced.com ServerName admin.yiiadvanced.com
Yii2 Advanced Default User
After restarting all services, we're up and running with http://yiiadvanced.com for our front end, and http://admin.yiiadvanced.com for the back end. Perfect.
Conclusion
In this episode, we went through all of the steps required to create the advanced template application structure with the Yii 2 Framework. We saw that it has built in support for both the public facing front end portion of the website, along with the administrator only backend version of the site. This provides a great way to bootstrap a new project.