Yii 2.0: yii2-improved-advanced-template

This template is remake of default advanced template, with extra functionalities included and is shared hosting friendly.
19 followers

Introduction

Yii2-advanced-template is based on yii2-app-advanced created by yii2 core developers. There are several upgrades made to this template.

  1. This template has additional features listed in the next section.
  2. Application structure has been changed to be 'shared hosting friendly'.

GitHub source: Click to see
Forum topic: Click to ask
Demo page: Click to try
Tutorial: Click to learn

Features

  • Signup with/without account activation
    • You can chose whether or not new users need to activate their account using email account activation system before they can log in. (see: backend/setting).
  • Login using email/password or username/password combo.
    • You can chose how users will login into system. They can log in either by using their username|password combo or email|password. (see: backend/setting).
  • Rbac tables are installed with other migrations when you run yii migrate command.
    • RbacController's init() action will insert 4 roles and 5 permissions in our rbac tables created by migration.
    • Roles can be easily assigned to users by administrators of the site (see: backend/user).
    • Nice example of how to use rbac in your code is given in this application. See: BackendController.
  • Users with editor+ roles can create articles.
  • Session data is stored in database out of the box.
  • System setting are stored in config/params.php file ( changes from v2 ).
  • Theming is supported out of box.
  • Translation is supported out of the box.
  • Administrators and The Creator can manage users ( changes from v2 ).
  • Password strength validation and strength meter.
  • All functionalities of default advanced template are included in this template.
  • Code is heavily commented out.

Installation

Note: I am assuming that you know how to: install and use Composer, and install additional packages/drivers that may be needed for you to run everything on your system. In case you are new to all of this, you can check my guides for installing default yii2 application templates on Windows and Linux Ubuntu/Mint operating systems, posted on http://www.freetuts.org .

1) Create database that you are going to use for your application (you can use phpMyAdmin or any other tool you like).

2) Now open up your console and cd to your web root directory, for example:

cd /var/www/sites/

3) Run the Composer create-project command:

composer create-project nenad/yii2-advanced-template advanced

4) Once template is downloaded, you need to initialize it in one of two environments: development (dev) or production (prod). Change your working directory to _protected and execute php init command:

cd advanced/_protected/
php init

Type 0 for development, execute command, type yes to confirm, and execute.

5) Now you need to tell your application to use database that you have previously created. Open up main-local.php config file in

advanced/_protected/common/config/main-local.php

and adjust your connection credentials.

6) Back to the console. It is time to run yii migrations that will create necessary tables in our database. While you are inside _protected folder execute ./yii migrate command:

./yii migrate

or if you are on Windows:

yii migrate

7) Execute rbac controller init action that will populate our rbac tables with default roles and permissions:

./yii rbac/init

or if you are on Windows:

yii rbac/init

You are done, you can start your application in your browser.
To see frontend side of your application visit this url in your localhost: localhost/advanced
To see backend side of your application visit this url in your localhost: localhost/advanced/backend

Info: First user that signs up will get 'The Creator' (super admin) role. This is supposed to be you. This role have all possible super powers :) . Every other user that signs up after the first one will get 'member' role. Member is just normal authenticated user.

Testing

If you want to run tests you should create additional database that will be used to store your testing data. Usually testing database will have the same structure like the production one. I am assuming that you have Codeception installed globally, and that you know how to use it. Here is how you can set up everything easily:

1) Let's say that you have created database called advanced. Go create the testing one called advanced_tests.

2) Inside your main-local.php config file change database you are going to use to advanced_tests.

3) Open up your console and cd to the _protected folder of your application.

4) Run the migrations again: ./yii migrate or if you are on Windows yii migrate

5) Run rbac/init again: ./yii rbac/init or if you are on Windows yii rbac/init

6) Now you can tell your application to use your advanced database again instead of advanced_tests. Adjust your main-local.php config file again.

7) Now you are ready to tell Codeception to use advanced_tests database.

Inside: _protected/tests/codeception/config/config.php file tell your db to use advanced_tests database.

8) Start your php server inside the root of your application:

php -S localhost:8080

9) To run tests written for frontend side of your application cd to _protected/tests/codeception/frontend , run codecept build and then run your tests.

10) Take similar steps like in step 9 for backend and common tests.

Directory structure

_protected
    backend
        assets/              contains backend assets definition
        config/              contains backend configurations
        controllers/         contains Web controller classes
        helpers/             contains helper classes
        models/              contains backend-specific model classes
        runtime/             contains files generated during runtime
        views/               contains view files for the Web application
    common
        config/              contains shared configurations
        mail/                contains view files for e-mails
        models/              contains model classes used in backend and frontend
        rbac/                contains role based access control classes
    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
    environments             contains environment-based overrides
    frontend
        assets/              contains frontend assets definition
        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
        widgets/             contains frontend widgets

assets                   contains application assets generated during runtime
backend                  contains the entry script and Web resources for backend
themes                   contains frontend themes

Version 2.0 changes

1) settings are stored in config/params.php configuration file to reduce database load
2) account update is merged with user management and user management is more powerful now
3) User model has been separated on UserIdentity and User (for easier understanding and use)
4) 4 beautiful bootstrap responsive themes are included out of the box
5) comment style is changed according to yii2 official style
6) tests has been rewritten according to the changes that has been made
7) a lot of other polishing has been done

Version 2.1.0 changes

1) option to CRUD articles ( posts ) has been added
2) translation support has been included and Serbian translation has been added
3) themes have been improved
4) new roles, permissions and rules are added
5) other code refactoring has been done

Version 2.2.0 changes

1) Adds uploads folder to the application root that can be shared by both frontend and backend applications.
2) Adds, @uploads alias has been added, so you can use it in your code ( will target your_app_name/uploads folder )
3) Additional translations are included. Thanks to MeFuMo and hior
4) Alert widget call is added to backend main.php layout
5) Minor fixes

Password strength guide

Since 1.1.1 version has been released, password strength extension has been included as a core part of improved templates. Usage is very simple:

In our signup, user create/update and password reset forms password strength meter is always displayed when users are entering their password. This will give them visual representation of their password strength.
But this is not all. As The Creator you have option in your settings "Force Strong Password" that you can use. If you turn it on, users will be forced to use strong passwords according to preset you chose. For example if you use normal preset, users will be forced to use at least 8 characters long password, with at least one upper-case and one lower-case letter, plus at least one digit.

Since version 2 settings are stored in config/params.php file!

Choosing presets:

By default normal preset is used for signup and user create/update forms. For password reset we are using 'reset' preset if you want to customize which presets is used, see SignupForm model, User model and ResetPasswordForm model. You will see rules declared for using strong passwords. Presets are located in vendor/nenad/yii2-password-strength/presets.php. You can chose some other preset declared in presets.php, or create new ones.

Change log

Version 2.2.0 - Summary is given in separate section of this guide.

Version 2.1.1 - Removes password reset link from backend login form.

Version 2.1.0 - Summary is given in separate section of this guide.

Version 2.0.2 - Adds fix for multiple flashes bug. Described here

Version 2.0.1 - Just few small cosmetic changes.

Version 2.0.0 - summary is given in separate section of this guide.

Version 1.2.3 - Optimizes password strength for mobile devices.

Version 1.2.2 - Fixes 404 issue that some people have on some systems.

Version 1.2.1 - Fixes issue #2 (for PostgreSQL users). Plus adds small typos and code fixes.

Version 1.2.0 - changes the way of using themes. Instead of specifying which theme you want to use in common/config/main.php, now you will have separate settings in frontend/config/main.php and backend/config/main.php where you will have to specify which theme you want to use for your frontend and which for backend application.

Total 19 comments

#18909 report it
Nenad Zivkovic at 2015/02/02 04:53pm
@Amjad Khan

Tutorial is here.

#18899 report it
Amjad Khan at 2015/02/01 10:51am
Tutorial on Rbac

You have implemented RBAC in this theme, its great, if you write some tutorial on how you Implement RBAC in this theme then it will be very helpful for Yii2 community.

Thanks

#18748 report it
Nenad Zivkovic at 2014/12/25 08:55pm
@Marcel Marnix

That was meant to be a part of much bigger system that I am going to create, but in the meantime I guess that I can make some tutorial about it. I am very busy these days, but as soon as I find some free time I will do it.

#18742 report it
Marcel Marnix at 2014/12/23 02:37am
Update Own Rule

Hello Nenad,

Thanks Your template helped me a lot!

Could you also implement a bizz rule. like update own.

Maybe you could create one simple table (posts; id, user_id, post) And then a member should be able to change his own post...

Then we (I) can also see how to implement a bizz rule!

Thanks in advance!!

#18527 report it
Arno S at 2014/11/10 07:18am
@Nenad

Yep, it's not entirely the same, I agree. The issues you mention are easily fixed though. I just wanted to show that Yii grids aren't hard to customize.

#18526 report it
Nenad Zivkovic at 2014/11/10 06:52am
@Arno S

In your controller you are missing code to prevent admin from seeing The Creator in Users list, admin should not mess with The Creator. Also in your view, role and status coloring are not displayed correctly, and role and status columns are not sortable. But you are going in good direction.

#18524 report it
Arno S at 2014/11/10 04:08am
Customizing the grid

I can respect the comment thing, although I disagree. You also made yourself a nice version of the grid. I do however feel that customizing the grid shouldn't take a lot of code. I've changed your users page with a dataprovider/grid combination like so:

//Controller code
$dataProvider = new \yii\data\ActiveDataProvider([
  'query' => User::find()->joinWith('role'),
  'pagination' => [
    'pageSize' => 20,
]]);
 
return $this->render('index', ['dataProvider'=>$dataProvider]);
 
//View code
echo GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            'id',
            'username',
            'email:email',
            [
                'attribute'=>'Status',
                'value'=>'statusName',
                'contentOptions'=>function($model, $key, $index, $column) {
                    return ['class'=>CssHelper::statusCss($model->status)];
                }
            ],
            [
                'attribute'=>'role',
                'value'=>'role.item_name',
                'contentOptions'=>function($model, $key, $index, $column) {
                    return ['class'=>CssHelper::roleCss($model->role)];
                }
            ],
            'created_at:datetime',
            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{update-role} {update} {delete}',
                'buttons' => [
                    'update-role' => function ($url, $model, $key) {
                        return User::STATUS_ACTIVE == $model->status ?
                            Html::a('', $url, ['title'=>'Update user role', 'class'=>'glyphicon glyphicon-user']) : '';
                    }
                ]
            ],
        ],
    ]);
#18518 report it
Nenad Zivkovic at 2014/11/09 06:30am
@Arno S

In my oppinion this style of comments makes code much more readable. I wanted to make code easy to read and follow for people who are using template. That is why I use different codding and commenting style. The one that Yii is officially using is so hard to read and follow.

Yii grids are hard to customize in the level I wanted them to look like. Like putting your own buttons, coloring roles and user status, boolean values and so on. Maybe you can customize all of that in yii grids, but it will require higher ammount of code than when you use your custom solution. My grids are good examples how you can use your custom code, without relying too much on Gii tool.

For our clients Users and Settings are very nicely displayed, easy to understand and use, which is our priority as developers.

For developers who use my templates code is easy to read and understand. All upgrades that you will make to template you can do however you like.

#18517 report it
Arno S at 2014/11/09 06:11am
Some comment

I like the change you made in the directory style, I find that a bit more logical. I do wonder why you chose to adopt a different commenting style (I'd stay close to how Gii generates it), not to use the Gridview widget and chose to implement your own variant of the Grid (in backend/users and settings) which seems unnecessarily complex.

Wouldn't it be better to keep as close as possible to the 'standard' Yii components (since you're advertising this as a template) to better 'teach' a starting user how simple these things can be done with the standard components?

#18512 report it
Nenad Zivkovic at 2014/11/08 01:28pm
Fix for issue explained by Arno S is included in template

Thank you Arno S for reporting this issue. I have included your fix in template, everyone will get it now.

#18510 report it
Arno S at 2014/11/08 12:43pm
Error in console commands

I had an issue trying to create new database migrations (yii migration/create). The console applications tries to use the theme which it can't locate. Fixed by disabling them in console config _protected/console/config/main.php

'components' => [
        'view' => null,
#18472 report it
Nenad Zivkovic at 2014/11/04 04:47am
@laozhuforever

I don't know exactly, I am doing all of this in my free time :D. I will do my best to make it as soon as posible. Maybe in about a month, maybe more... I will not release it until I do it right.

#18470 report it
laozhuforever at 2014/11/03 08:54pm
@Nenad Zivkovic

Thanks for your templates. I pay more attention to your new templates about e-commerce. AWESOME! When it will be released?

#18466 report it
Nenad Zivkovic at 2014/11/03 10:55am
@jokamax

That doesn't really depend on my templates, it depends on third party tools you are using... If you can integrate them into default yii2 template, then you will not have any problems integrating them here too.

#18465 report it
jokamax at 2014/11/03 09:51am
How To

Thanks. Indeed, after thinking about, maybe a simple HowTo integrate the third part in your templates (globally) would be enough ^^

#18464 report it
Nenad Zivkovic at 2014/11/03 09:36am
@jokamax

Ok I get it.

Since in many applications providing web service is not needed, maybe I can create some template that is specialized in providing Restful web service beside normal web application.

First, I want to create blog and e-commerce templates, after I am done with that, I will consider creating template that is specialized for working with REST services. Or even better, maybe I can create REST web service module that can be used in any other web application. I dont really know yet, because I have some other priorities at this moment, but I will have this in mind, it is good idea. You will be informed, thank your for your feedback.

#18463 report it
jokamax at 2014/11/03 07:14am
@Nenad

yii2 is restful by default but the official guide advise to create a third part (like frontend & backend).
https://github.com/yiisoft/yii2/blob/master/docs/guide/rest-quick-start.md
I'd love having this third part (simple) architectured in this template (even if, this is just a skelton or with configuration in comments)

Thanks !

#18462 report it
Nenad Zivkovic at 2014/11/03 06:56am
@jokamax

What exactly would you like to see in this template ?

#18461 report it
jokamax at 2014/11/03 04:18am
Very Interesting

I'll try this as soon as possible (if you add a part - optionnal - for REST API in addition of Frontend & Backend, that would be paradise ^^).

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet