1. 程式人生 > >yii2 basic版本的一些配置

yii2 basic版本的一些配置

1.nginx配置 重寫規則 修改訪問模式為 http://wh.store/admin/index

檔案位置: /home/wwwroot/default/yii2-app-basic/config/web.php

'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],

檔案位置: /usr/local/nginx/conf/vhost/yz.store.conf

server
{
listen 80;
#listen [::]:80;
server_name yz.store ;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/default/yii2-app-basic/web;

    include none.conf;
    #error_page   404   /404.html;

    # Deny access to PHP files in specific directory
    #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

    include enable-php.conf;

    access_log  /home/wwwroot/default/yii2-app-basic/vagrant/nginx/log/y.net.access.log;
    error_log   /home/wwwroot/default/yii2-app-basic/vagrant/nginx/log/y.net.error.log;

    location / {
      # Redirect everything that isn't a real file to index.php
      try_files $uri $uri/ /index.php$is_args$args;
    }

   # deny accessing php files for the /assets directory
   location ~ ^/assets/.*\.php$ {
     deny all;
   }

   location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass 127.0.0.1:9000;
    #fastcgi_pass unix:/var/run/php5-fpm.sock;
    try_files $uri =404;
   }

   location ~* /\. {
    deny all;
   }        

}

2. FastCGI sent in stderr: "PHP message: PHP Warning: require(): open_basedir restriction in effect.

修改nginx 配置 /usr/local/nginx/conf/fastcgi.conf # 禁用 重啟伺服器

#fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";

3.post方式不能訪問 post 400 csrf json請求不到

禁用csrf 配置json請求

檔案地址:/home/wwwroot/default/yii2-app-basic/config/web.php

'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'mark453100',
            "enableCsrfValidation"=>false, #禁用csrf
            "parsers" => [
               'application/json' => 'yii\web\JsonParser',   #支援json格式的請求
               'text/json' => 'yii\web\JsonParser', #支援json格式的請求
            ],

        ]

控制器接收post方法

$request = Yii::$app->request;
$post = $request->post();
var_dump($post);

4.如何配置資料庫和建立模型類

檔案地址:/home/wwwroot/default/yii2-app-basic/config/db.php

<?php
return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=192.168.1.125;dbname=h5_store',
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8',
    'tablePrefix' => 'h5_', #表字首
    // Schema cache options (for production environment)
    //'enableSchemaCache' => true,
    //'schemaCacheDuration' => 60,
    //'schemaCache' => 'cache',
];

Model類

檔案位置:/home/wwwroot/default/yii2-app-basic/models/Product.php

<?php

namespace app\models;

use yii\db\ActiveRecord;

class Product extends ActiveRecord
{
    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;

    /**
     * @return string AR 類關聯的資料庫表名稱
     */
    public static function tableName()
    {
        return '{{product}}';
    }
}