ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

yii2使用gii模块的最低条件

2021-01-26 12:33:36  阅读:190  来源: 互联网

标签:files php uri try .+ location 模块 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有