This How-To describes setting up a Docker stack with the Yii2 Basic App template.
The application will use a MariaDB database and activate a users management module.
All example commands below were entered in a Windows Command shell.
Note:
When you have to enter commands spanning over multiple lines, on Windows you have to enter `^` at the end of command lines instead of the backslash `\` shown below.
$ git clone https://github.com/yiisoft/yii2-app-basic.git my-yii-app $ cd my-yii-app
$ docker-compose run --rm php composer update --prefer-dist
$ docker-compose run --rm php composer install
$ docker-compose up -d
You can then access the application through the following URL:
$ docker-compose stop
docker-compose.yaml
:version: '3' services: php: image: yiisoftware/yii2-php:7.3-apache volumes: - ~/.composer-docker/cache:/root/.composer/cache:delegated - ./:/app:delegated ports: - '8000:80' links: - mariadb mariadb: image: mariadb:10.1 volumes: - mariadb:/var/lib/mysql environment: TZ: "Europe/Berlin" MYSQL_ALLOW_EMPTY_PASSWORD: "no" MYSQL_ROOT_PASSWORD: "rootpw" MYSQL_USER: 'testuser' MYSQL_PASSWORD: 'testpassword' MYSQL_DATABASE: 'testdb' volumes: mariadb:
db.php
:<?php return [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=mariadb;dbname=testdb', 'username' => 'testuser', 'password' => 'testpassword', 'charset' => 'utf8', ];
Adminer is a handy script to manage databases, tables and data.
docker-compose.yaml
:services ... adminer: image: adminer restart: always ports: - 8081:8080
Now you can open the Adminer app in http://127.0.0.1:8081/?server=mariadb&username=testuser&db=testdb.
Server: mariadb Username: testuser Password: testpassword Database: testdb
[Login]
.Yii2-user is a flexible user registration and authentication module for Yii2.
$ docker-compose run php composer require dektrium/yii2-user
config/web.php
:return [ ... 'components' => [ ... /* Comment out to use dektrium user model: 'user' => [ ... ], */ ... ], 'modules' => [ 'user' => [ 'class' => 'dektrium\user\Module', 'enableFlashMessages' => false, // Don't print flash messages by the module, the app handles printing already ], ], ... ];
$ docker-compose run php yii migrate/up \ --migrationPath=@vendor/dektrium/yii2-user/migrations
In order to register yourself as a first user, open http://127.0.0.1:8000/index.php?r=user/registration/register .
In the examples below, we use theadminuser as an example username.
runtime/mail/
for a new email file (*.eml)The link looks something like this::
http://127.0.0.1:8000/index.php?r=user%2Fregistration%2Fconfirm&id=1&code=…
The application is opened and a confirmation message is shown:
Thank you, registration is now complete.
Now some links regarding user functionality in the page template need to be changed, e.g.
views/layout/main.php
:// Setup menu items $menuItems = []; $menuItems[] = ['label' => 'Home', 'url' => ['/site/index']]; if(!Yii::$app->user->isGuest and in_array(Yii::$app->user->identity->username, Yii::$app->params['userAdmins'])) { $menuItems[] = ['label' => 'Users Admin', 'url' => ['/user/admin']]; } $menuItems[] = ['label' => 'About', 'url' => ['/site/about']]; $menuItems[] = ['label' => 'Contact', 'url' => ['/site/contact']]; if (Yii::$app->user->isGuest) { $menuItems[] = ['label' => 'Signup', 'url' => ['/user/registration/register']]; $menuItems[] = ['label' => 'Login', 'url' => ['/user/security/login']]; } else { $menuItems[] = ['label' => 'Settings', 'url' => ['/user/settings']]; $menuItems[] = ['label' => 'Logout (' . Yii::$app->user->identity->username . ')', 'url' => ['/user/security/logout'], 'linkOptions' => ['data-method' => 'post']]; } ... echo Nav::widget([ 'options' => ['class' => 'navbar-nav navbar-right'], // Change: 'items' => $menuItems, ]);
The yii2-user module provides an administration view to manage users.
In order to access it, you need to configure a list of usernames who are allowed to use the view.
config/params.php
:<?php return [ ... // Users Module / Admin usernames 'userAdmins' => ['theadminuser'], ];
config/web.php
:... 'modules' => [ 'user' => [ 'class' => 'dektrium\user\Module', // Admin users: see config/params.php 'admins' => $params['userAdmins'], ], ], ...
You may now open the user administration screen using the URL
http://127.0.0.1:8000/index.php?r=user/admin
You will see a GridView with already registered users. From here you can edit users or create new ones.