这是基于Yii2 Starter Kit 和 Yii2 Advanced Template 的开箱即用
的Web 应用程序。
##安装
git clone https://git.oschina.net/kzeng/yii2-app-kit.git
已集成,此步可略过
>composer install
The minimum requirement by this application template that your Web server supports PHP 5.5.0. Required PHP extensions:
以lnmp 1.2 PHP服务器套件为例:
cd /home/freeman/tools/lnmp1.2-full/src/php-5.6.9/ext/intl/
$phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install
在这个configure的过程中会出现错误,安装相应的lib就行。
$yum install libicu-devel -y
$yum install libxslt-devel -y
cd /home/freeman/tools/lnmp1.2-full/src/php-5.6.9/ext/xsl/
$phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install
修改php.ini
$vi /usr/local/php/etc/php.ini
增加:
extension=xsl.so
extension=intl.so
然后重启php服务/etc/init.d/php-fpm restart
.env.dist
to .env
in the project root (.env.docker.dist
if you are using docker).env
file
YII_DEBUG = true
YII_ENV = dev
- Set DB configuration
DB_DSN = mysql:host=127.0.0.1;port=3306;dbname=yii2-app-kit
DB_USERNAME = user
DB_PASSWORD = password
- Set application canonical urls
FRONTEND_URL = http://yii2-app-kit.dev
BACKEND_URL = http://backend.yii2-app-kit.dev
STORAGE_URL = http://storage.yii2-app-kit.dev
此步必须, 重要!
php console/yii app/setup
php console/yii migrate --migrationPath=vendor/pendalf89/yii2-filemanager/migrations
Copy vhost.conf.dist
to vhost.conf
, change it with your local settings and copy (symlink) it to nginx sites-enabled
directory.
Or configure your web server with three different web roots:
Adjust settings in .env
file
FRONTEND_URL = /
BACKEND_URL = /admin
STORAGE_URL = /storage/web
Adjust settings in backend/config/web.php
file
...
'components'=>[
...
'request' => [
'baseUrl' => '/admin',
...
Adjust settings in frontend/config/web.php
file
...
'components'=>[
...
'request' => [
'baseUrl' => '',
...
This is an example single domain config for apache
<VirtualHost *:80>
ServerName yii2-app-kit.dev
RewriteEngine on
# the main rewrite rule for the frontend application
RewriteCond %{HTTP_HOST} ^yii2-app-kit.dev$ [NC]
RewriteCond %{REQUEST_URI} !^/(backend/web|admin|storage/web)
RewriteRule !^/frontend/web /frontend/web%{REQUEST_URI} [L]
# redirect to the page without a trailing slash (uncomment if necessary)
#RewriteCond %{REQUEST_URI} ^/admin/$
#RewriteRule ^(/admin)/ $1 [L,R=301]
# disable the trailing slash redirect
RewriteCond %{REQUEST_URI} ^/admin$
RewriteRule ^/admin /backend/web/index.php [L]
# the main rewrite rule for the backend application
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^/admin(.*) /backend/web$1 [L]
DocumentRoot /your/path/to/yii2-app-kit
<Directory />
Options FollowSymLinks
AllowOverride None
AddDefaultCharset utf-8
</Directory>
<Directory "/your/path/to/yii2-app-kit/frontend/web">
RewriteEngine on
# if a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule . index.php
Require all granted
</Directory>
<Directory "/your/path/to/yii2-app-kit/backend/web">
RewriteEngine on
# if a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule . index.php
Require all granted
</Directory>
<Directory "/your/path/to/yii2-app-kit/storage/web">
RewriteEngine on
# if a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule . index.php
Require all granted
</Directory>
<FilesMatch \.(htaccess|htpasswd|svn|git)>
Require all denied
</FilesMatch>
</VirtualHost>
This is an example single domain config for nginx
server {
listen 80;
root /app;
index index.php index.html;
server_name yii2-app-kit.dev;
charset utf-8;
# location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|pdf|ppt|txt|bmp|rtf|js)$ {
# access_log off;
# expires max;
# }
location / {
try_files $uri /frontend/web/index.php?$args;
}
location /backend {
try_files $uri /backend/web/index.php?$args;
}
# storage access
location /storage {
try_files $uri /storage/web/index.php?$args;
}
client_max_body_size 32m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm;
fastcgi_index index.php;
include fastcgi_params;
## Cache
# fastcgi_pass_header Cookie; # fill cookie valiables, $cookie_phpsessid for exmaple
# fastcgi_ignore_headers Cache-Control Expires Set-Cookie; # Use it with caution because it is cause SEO problems
# fastcgi_cache_key "$request_method|$server_addr:$server_port$request_uri|$cookie_phpsessid"; # generating unique key
# fastcgi_cache fastcgi_cache; # use fastcgi_cache keys_zone
# fastcgi_cache_path /tmp/nginx/ levels=1:2 keys_zone=fastcgi_cache:16m max_size=256m inactive=1d;
# fastcgi_temp_path /tmp/nginx/temp 1 2; # temp files folder
# fastcgi_cache_use_stale updating error timeout invalid_header http_500; # show cached page if error (even if it is outdated)
# fastcgi_cache_valid 200 404 10s; # cache lifetime for 200 404;
# or fastcgi_cache_valid any 10s; # use it if you want to cache any responses
}
}
upstream php-fpm {
server fpm:9000;
}
administrator
role account
Login: webmaster
Password: webmaster
manager
role account
Login: manager
Password: manager
user
role account
Login: user
Password: user
sendfile off;
guest
, user
, manager
and administrator
rolesExtendedMessageController
with ability to replace source code language and migrate messages between message sourcesIf you want to store application messages in DB and to have ability to edit them from backend, run:
php console/yii message/migrate @common/config/messages/php.php @common/config/messages/db.php
it will copy all existing messages to database
Then uncomment config for DbMessageSource
in
common/config/base.php
Key storage is a key-value storage to store different information. Application settings for example. Values can be stored both via api or by backend CRUD component.
Yii::$app->keyStorage->set('articles-per-page', 20);
Yii::$app->keyStorage->get('articles-per-page'); // 20
Application kit has built-in component to provide a maintenance functionality. All you have to do is to configure maintenance
component in your config
'bootstrap' => ['maintenance'],
...
'components' => [
...
'maintenance' => [
'class' => 'common\components\maintenance\Maintenance',
'enabled' => Astronomy::isAFullMoonToday()
]
...
]
This component will catch all incoming requests, set proper response HTTP headers (503, "Retry After") and show a maintenance message. Additional configuration options can be found in a corresponding class.
Application kit configured to turn on maintenance mode if frontend.maintenance
key in KeyStorage is set to true
In Application Kit Command Bus pattern is implemented with tactician package and it's yii2 connector - yii2-tactician
Command are stored in common/commands/command
directory, handlers in common/commands/handler
To execute command run
$sendEmailCommand = new SendEmailCommand(['to' => 'user@example.org', 'body' => 'Hello User!']);
Yii::$app->commandBus->handle($sendEmailCommand);
$addToTimelineCommand = new AddToTimelineCommand([
'category' => 'user',
'event' => 'signup',
'data' => ['foo' => 'bar']
]);
Yii::$app->commandBus->handle($addToTimelineCommand);
public function behaviors()
{
return [
[
'class' => `common\behaviors\CacheInvalidateBehavior`,
'tags' => [
'awesomeTag',
function($model){
return "tag-{$model->id}"
}
],
'keys' => [
'awesomeKey',
function($model){
return "key-{$model->id}"
}
]
],
];
}
Add in your application config:
'as globalAccess'=>[
'class'=>'\common\behaviors\GlobalAccessBehavior',
'rules'=>[
[
'controllers'=>['sign-in'],
'allow' => true,
'roles' => ['?'],
'actions'=>['login']
],
[
'controllers'=>['sign-in'],
'allow' => true,
'roles' => ['@'],
'actions'=>['logout']
],
[
'controllers'=>['site'],
'allow' => true,
'roles' => ['?', '@'],
'actions'=>['error']
],
[
'allow' => true,
'roles' => ['@']
]
]
]
It will allow access to you application only for authentificated users.
Read more about command bus on in official repository
<?php echo DbCarousel::widget(['key' => 'key-from-backend']) ?>
<?php echo DbText::widget(['key' => 'key-from-backend']) ?>
<?php echo DbMenu::widget(['key' => 'key-from-backend']) ?>
[
'class' => '\common\grid\EnumColumn',
'attribute' => 'status',
'enum' => User::getStatuses() // [0=>'Deleted', 1=>'Active']
]
Application Kit has fully configured and ready-to-go REST API module. You can access it on http://yii2-app-kit.dev/api/v1
common\base\MultiModel
- class for handling multiple models in one
In controller:
$model = new MultiModel([
'models' => [
'user' => $userModel,
'profile' => $userProfileModel
]
]);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
...
}
In view:
<?php echo $form->field($model->getModel('account'), 'username') ?>
<?php echo $form->field($model->getModel('profile'), 'middlename')->textInput(['maxlength' => 255]) ?>
common\behaviors\GlobalAccessBehavior
- allows to set access rules for your application in application config
common\behaviors\LocaleBehavior
- discover user locale from browser or account settings and set it
common\behaviors\LoginTimestampBehavior
- logs user login time
common\validators\JsonValidator
- validates a value to be a valid json
common\rbac\rule\OwnModelRule
- simple rule for RBAC to check if the current user is model owner
Yii::$app->user->can('editOwnModel', ['model' => $model]);
common\filters\OwnModelAccessFilter
- action filter to check if user is allowed to manage this modelpublic function behaviors()
{
return [
'modelAccess' => [
'class' => OwnModelAccessFilter::className(),
'only' => ['view', 'update', 'delete'],
'modelClass' => Article::className()
],
];
}
##CONSOLE
Provides migrate functionality for RBAC.
php console/yii rbac-migrate/create init_roles
php console/yii rbac-migrate/up
php console/yii rbac-migrate/down all
You need to have yuicompressor and uglifyjs installed.
php console/yii asset/compress frontend/config/assets/compress.php frontend/config/assets/_bundles.php
then uncomment these lines in the frontend/config/web.php
// Compressed assets
//$config['components']['assetManager'] = [
// 'bundles' => require(__DIR__ . '/assets/_bundles.php')
//];
##测试
To run tests:
tests/.env.dist
to tests/.env
and edit it to set your local settingsyii2-app-kit-test
databasephp tests/codeception/bin/yii app/setup
php -S localhost:8080
codecept run
##常见问题
Gii is available on:
Edit frontend/config/web.php
and set [[frontend\modules\user\Module::shouldBeActivated]] property to true
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。