• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Mark的小试牛刀

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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$

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}}';
    }
}

posted on 2018-04-11 18:23  我是天才啊  阅读(351)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3