其他分享
首页 > 其他分享> > yii2使用gii模块的最低条件

yii2使用gii模块的最低条件

作者:互联网

因为想改变yii2的使用方式,改成yii2作为微服务,而前端使用前端框架之类的完成,但同时,我又想保留gii的使用,以便快速生成model和restful风格的controller,所以,就研究使用gii最低需要哪些条件?

显然,gii模块其实是使用了yii2的basic或advanced模板一样捆绑的jquery和bootstrap的,所以,我们事实上还是需要回到老路的。

在我的项目jsjxinfo下建立应用目录my,my下创建web目录作为该应用的文档根,里面放上入口文件index.php和空文件夹assets(运行时发布资源用)。在my下建立runtime目录和controllers目录,controllers目录下放SiteController(使用controllers目录是测试配置用的),不需要models和views目录。SiteController.php如下:

<?php

namespace my\controllers;

use yii\web\Controller;

class SiteController extends Controller
{
    public function actionIndex()
    {
        return 'my site index';
    }
}

因为需要引入jquery等资源,所以,my下建立assets目录并创建AppAsset.php,内容如下:

<?php

namespace my\assets;

use yii\web\AssetBundle;

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [];
    public $js = [];
    public $depends = [
        'yii\web\YiiAsset',
    ];
}

我们希望打造自己的gii模板文件,所以,在my下创建giiTemplate目录(里面可以存放model等和gii对应的目录)。最关键的文件当然是入口文件index.php:

<?php

require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/../../vendor/yiisoft/yii2/Yii.php';

Yii::setAlias('@my', dirname(__DIR__));
// 以下为各个应用对应根目录
Yii::setAlias('@lab', dirname(__DIR__).'/../lab');
Yii::setAlias('@lock', dirname(__DIR__).'/../lock');

$app = (new yii\web\Application([
    'id' => 'my-gii',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'my\controllers',
    'vendorPath' => dirname(dirname(__DIR__)).'/vendor',
    'bootstrap' => ['gii'],
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    'modules' => [
        'gii' => [
            'class' => 'yii\gii\Module',
//            'generators' => [
//                'model' => [
//                    'class' => 'yii\gii\generators\model\Generator',
//                    'template' => [
//                        'restful' => '@app/../giiTemplate/model/default',
//                    ]
//                ]
//            ]
        ]
    ],
    'components' => [
        'request' => [
            'cookieValidationKey' => 'this key must used',
        ],
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=jsjxinfo',
            'username' => 'jsjxinfo',
            'password' => '*******',
            'charset' => 'utf8',
            // 'enableSchemaCache' => true,
        ]
    ]
]));
$app->run();

对应的nginx配置部分:

server	{
	charset utf-8;
	client_max_body_size 128M;

	listen 8080;
	listen [::]:8080;

	server_name jsjxinfo;
	
	access_log /home/zime/PhpstormProjects/jsjxinfo/log/access.log;
	error_log /home/zime/PhpstormProjects/jsjxinfo/log/error.log info;

	set $prj_root /home/zime/PhpstormProjects/jsjxinfo;
	root $prj_root;

	location / {
	    try_files $uri /web/index.html?$args;
	    location ~ ^\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
	        access_log off;
		expires 360d;
		try_files $uri =404;
	    }
	}

	location /admin {
	    rewrite ^(/admin)/$ $1 permanent;
	    try_files $uri /admin/index.html?$args;
	    location ~  ^\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
	        access_log off;
		expires 360d;
		try_files $uri =404;
	    }
	}

	location /h5 {
	    rewrite ^(/h5)/$ $1 permanent;
	    try_files $uri /h5/index.html?$args;
	    location ~ ^\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
	        access_log off;
		expires 360d;
		try_files $uri =404;
	    }
	}

	location /my {
	    rewrite ^(/my)/$ $1 permanent;
	    try_files $uri index.php?$args /my/web/index.php?$args;
	}

	location ~ ^/my/(.+\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar))$ {
	    access_log off;
	    expires 360d;

	    rewrite ^/lab/(.+)$ /lab/web/$1 break;
	    rewrite ^/lab/(.+)/(.+)$ /lab/web/$1/$2 break;
	    try_files $uri =404;
	}
	
	location /lab {
	   rewrite ^(/lab)/$ $1 permanent;
	   try_files $uri index.php?$args  /lab/web/index.php?$args;
	}

	location ~ ^/lab/(.+\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar))$ {
	    access_log off;
	    expires 360d;

	    rewrite ^/lab/(.+)$  /lab/web/$1 break;
	    rewrite ^/lab/(.+)/(.+)$ /lab/web/$1/$2 break;
	    try_files $uri =404;
	}


	location /lock {
	    rewrite ^(/lock)/$ $1 permanent;
	    try_files $uri /lock/web/index.php?$args;
	}

	location ~ ^/lock/(.+\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar|woff2|svg|eot|woff|ttf))$ {
	    access_log off;
	    expires 360d;

	    rewrite ^/lock/(.+)$  /lock/web/$1 break;
	    rewrite ^/lock/(.+)/(.+)$ /lock/web/$1/$2 break;
	    try_files $uri =404;
	}

	location /device {
	    rewrite ^(/device)/$ $1 permanent;
	    try_files $uri /device/web/index.php?$args;
	}

	location ~ ^/device/(.+\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar|woff2|svg|eot|woff|ttf))$ {
	    access_log off;
	    expires 360d;

	    rewrite ^/device/(.+)$  /device/web/$1 break;
	    rewrite ^/device/(.+)/(.+)$ /device/web/$1/$2 break;
	    try_files $uri =404;
	}

	

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

	# pass PHP scripts to FastCGI server
	#
	location ~ \.php$ {

	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
		fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;

		fastcgi_connect_timeout 300;
		fastcgi_read_timeout 300;
		fastcgi_send_timeout 300;

		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include  fastcgi_params;
		try_files $uri =404;
	}

	location = /requirements.php {
	    deny all;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.(ht|svn|git) {
		deny all;
	}
}

待完善

标签:files,php,uri,try,.+,location,模块,yii2,gii
来源: https://blog.csdn.net/sjg20010414/article/details/113177361