yii2 basic版本的一些配置

时间:2022-04-21 09:08:49

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