Фреймворк Yii2
Yii2 Установка
Yii2 может быть установлена как basic или advanced.
- basic — структура постого сайта.
- advanced — структура папок разнесена на frontend и backend части.
Перед установкой любой из версий выполняем:
composer global require "fxp/composer-asset-plugin:^1.2.0"
Устанавливаем basic:
composer create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic my_project_dir_name
Устанавливаем advanced:
composer create-project --prefer-dist yiisoft/yii2-app-advanced my_project_dir_name
Yii2 basic настройка Apache
После установки, в вашем каталоге появятся файлы и каталоги:
├── assets ├── codeception.yml ├── commands ├── composer.json ├── composer.lock ├── config ├── controllers ├── LICENSE.md ├── mail ├── models ├── README.md ├── requirements.php ├── runtime ├── tests ├── vendor ├── views ├── web ├── yii └── yii.bat
Базовый каталог, на который нужно настроить сервер — web
DocumentRoot "path/to/basic/my_project_dir_name/web"
RewriteEngine on
# Если запрашиваемая в URL директория или файл существуют обращаемся к ним напрямую
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Если нет - перенаправляем запрос на index.php
RewriteRule . index.php
Файл .htaccess
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# Если запрос не начинается с web, добавляем его
RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule (.*) /web/$1
# Если файл или каталог не существует, идём к /web/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php
Yii2 basic настройка Nginx
Базовый каталог, на который нужно настроить сервер — web
server {
server_name my-yii2-basic;
root /path/to/basic/my_project_dir_name/web;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log /path/to/basic/logs/project_error.log;
access_log /path/to/basic/my_project_dir_name/web/project_access.log;
}
Yii2 basic настройка фреймвока
открываем файл config/db.php и указываем параметры подключения.
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2basic',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];
Красивые url (чпу)
открываем файл config/web.php
находим и раскомментируем следующий код
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],