Yii v2 for beginners

You are viewing revision #24 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.

« previous (#23)next (#25) »

  1. Intro
  2. Prerequisities
  3. Yii demo app + GitLab
  4. Translations (i18n) + changing languages
  5. User management + basic SQL commands
  6. Login via database + Session
  7. Access rights

Please give me a few days to add whole text

Intro

Skip this paragraph if you are in hurry :-) ... 8 years ago I started these two tutorials for Yii 1:

... and today I am beginning with Yii 2 so I will also gather my snippets and publish them here so we all can quickly setup the yii-basic-demo.

I have some experiences with Yii 1, but I havent used Yii for almost 5 years so many things are new for me again. Plus I was suprised that the Yii 2 demo application does not contain some basic functionalities (like DB login, translations etc) which must be implemented in the most of web projects so I will focus on them. Plus I will talk about GitLab.

Prerequisities

Skip this paragraph if you know how to run your Yii demo project...

I work with Win10 + XAMPP Server so I will expect this configuration. Do not forget to start the server and enable Apache + MySQL in the dialog. Then test that following 2 URLs work for you

You should also download the Yii basic demo application and place it into the htdocs folder. In my case it is here:

  • C:\xampp\htdocs

And your index.php should be here:

  • C:\xampp\htdocs\basic\web\index.php

If you set thing correcly up, following URL will open your demo application. Now it will probably throw an exception:

The Exception is removed by enterinng any text into attribute 'cookieValidationKey' in file:

  • C:\xampp\htdocs\basic\config\web.php

Dont forget to connect Yii to the DB. It is done in file:

  • C:\xampp\htdocs\basic\config\db.php

... but it should work out of the box ...

Yii demo app + GitLab

... text ...

Translations (i18n) + changing languages

... text ...

User management + basic SQL commands

To create DB with users, use following command. I recommend charset utf8_unicode_ci (or utf8mb4_unicode_ci) as it allows you to use more international characters.

CREATE DATABASE IF NOT EXISTS `yii2basic` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

CREATE TABLE IF NOT EXISTS `user` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(45) NOT NULL,
  `password` VARCHAR(60) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

INSERT INTO `user` (`id`, `username`, `password`) VALUES (NULL, 'user01', '0497fe4d674fe37194a6fcb08913e596ef6a307f');

If you must use MyISAM instead of InnoDB, just change the word InnoDB into MYISAM.

Then use GII to generate model, views and controller. The URL will probably be

Login via database + Session

... text ...

Access rights

... text ...