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
<?php
return [
'components' => [
'db' => [
'class' => 'yiidbConnection',
'dsn' => 'mysql:host=localhost;dbname=yii2advanced',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'mailer' => [
'class' => 'yiiswiftmailerMailer',
'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
],
];
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
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
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.
DocumentRoot “C:/wamp/www/yii/advanced/frontend/web”
ServerName yiiadvanced.com
DocumentRoot “C:/wamp/www/yii/advanced/backend/web”
ServerName admin.yiiadvanced.com
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.