yii安装高级版及前后台用户分离

1.下载advanced

2.进入advanced

  cd advanced

3.初始化

  php init

4.新建数据库yii2advanced

  数据库配置common/config/main-local.php

  用migrate新建表

    yii migrate

5.配置nginx

frontend

server {
    charset utf-8;
        client_max_body_size 128M;
        listen       80;
        server_name  127.0.0.55;  
    root   "E:/vhost/yiidemo/advanced/frontend/web/";
    access_log  E:/vhost/yiidemo/advanced/log/frontend-access.log;
        error_log   E:/vhost/yiidemo/advanced/log/frontend-error.log;
        location / {    
            index  index.php index.html index.htm  l.php;
            try_files $uri $uri/ /index.php$is_args$args;
        }
    location ~ ^/assets/.*\.php$ {
            deny all;
        }
    location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
            try_files $uri =404;
        }
        location ~* /\. {
            deny all;
        }    
    }

浏览器访问127.0.0.55,可以注册用户了

backend

server {
    charset utf-8;
        client_max_body_size 128M;
        listen       80;
        server_name  127.0.0.56;  
    root   "E:/vhost/yiidemo/advanced/backend/web/";
    access_log  E:/vhost/yiidemo/advanced/log/backend-access.log;
        error_log   E:/vhost/yiidemo/advanced/log/backend-error.log;
        location / {    
            index  index.php index.html index.htm  l.php;
            try_files $uri $uri/ /index.php$is_args$args;
        }
    location ~ ^/assets/.*\.php$ {
            deny all;
        }
    location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
            try_files $uri =404;
        }
        location ~* /\. {
            deny all;
        }    
    }

浏览器访问127.0.0.56,用刚才注册的用户可以登录

但是,现在用的是一套用户,前后台的用户应该做分离

6.前后用户台分离

创建migration

  yii migrate/create admin

仿照init文件

use yii\db\Migration;
class m171009_081713_admin extends Migration
{
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable('{{%admin}}', [
            'id' => $this->primaryKey(),
            'username' => $this->string()->notNull()->unique(),
            'auth_key' => $this->string(32)->notNull(),
            'password_hash' => $this->string()->notNull(),
            'password_reset_token' => $this->string()->unique(),
            'email' => $this->string()->notNull()->unique(),
            'status' => $this->smallInteger()->notNull()->defaultValue(10),
            'created_at' => $this->integer()->notNull(),
            'updated_at' => $this->integer()->notNull(),
        ], $tableOptions);
    }

    public function down()
    {
        $this->dropTable('{{%admin}}');
    }
}

执行

  yii migrate

(a)前台修改:

  先把公用的common/models中的User.php和LoginForm.php移动到frontend/models中去

  将这两个文件的命名空间改为以frontend开头,将整个前台文件看一遍,把所有涉及到这两个common文件命名空间的需要都改为前台自己的命名空间

(b)后台修改:

  在backend/models中有这两个文件Admin.php和LoginForm.php

  可以使用Gii生成(需要注意要继承IdentityInterface,实现此接口内的方法以及参照User.php来实现相关登录注册方法)

  也可以直接复制同样上面的两个文件(需要将User.php改名为Admin.php,LoginForm.php中的User改为Admin,修改命名空间)

7.用console的功能来创建一个后台管理员

在console/controllers中新建InitController

<?php
namespace console\controllers;
use Yii;
use yii\console\Controller;
use backend\models\Admin;
class InitController extends Controller { public function actionAdmin() { echo "Create init admin ...\n"; // 提示当前操作 $username = $this->prompt('Admin Name:');// 接收用户名 $email = $this->prompt('Email:'); // 接收Email $password = $this->prompt('Password:'); // 接收密码 $model = new Admin(); // 创建一个新用户 $model->username = $username; // 完成赋值 $model->email = $email; $model->password = $password; //注意这个地方,用了Admin模型中的setPassword方法(魔术方法__set) if (!$model->save()) // 保存新的用户 { foreach ($model->getErrors() as $error)// 如果保存失败,说明有错误,那就输出错误信息。 { foreach ($error as $e) { echo "$e\n"; } } return 1; // 命令行返回1表示有异常 } return 0; // 返回0表示一切OK } }
执行
  yii init/admin

按照提示来填写用户名密码等,便可以产生一条管理员数据

8.身份验证

在backend/config/main.php

'components' => [
        'request' => [
            'csrfParam' => '_csrf-backend',
        ],
        'user' => [
            'identityClass' => 'backend\models\Admin',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
        ],

在frontend/config/main.php

'components' => [
        'request' => [
            'csrfParam' => '_csrf-frontend',
        ],
        'user' => [
            'identityClass' => 'frontend\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
        ],

到此为止,前后台完全没关联了

posted @ 2017-10-09 16:57  慕尘  阅读(271)  评论(0编辑  收藏  举报