·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> yii2 get started

yii2 get started

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
yii2 get startedYii2 提供的基本模板有2种
  • yii2-app-basic
  • yii2-app-advanced

    • console
    • backend
    • frontend
    • ...
对php版本的要求

Yii2 要求 PHP 版本在 PHP 5.4.0 之上

eg. yii2 中用到这样的代码 php 5.3.5 就不支持

$extension = ['name' => $package->getName(),'version' => $package->getVersion(),];

如果之前使用的是 PHP5.3 版本 要切换到 PHP5.4, 可以参考我的另一篇笔记

php5.3 php5.4 install and config on windows VC6 and VC9

原创文章 http://www.cnblogs.com/ganiks/

下面对 yii2 的 2 种模板分别进行 Install 和 RunInstall Yii2 advanced via Composer

If you do not have Composer, you may install it by following the instructions at getcomposer.org.

C:\Documents and Settings\ganiks.liu>php -vPHP 5.4.30 (cli) (built: Jun 25 2014 18:06:41)Copyright (c) 1997-2014 The PHP GroupZend Engine v2.4.0, Copyright (c) 1998-2014 Zend TechnologiesC:\Documents and Settings\ganiks.liu>e:E:\>composer  create-PRoject --prefer-dist --stability=dev yiisoft/yii2-app-advanced advancedInstalling yiisoft/yii2-app-advanced (dev-master f7e31c08c47124bda8fecd596d94f3dc4c1d5218)  - Installing yiisoft/yii2-app-advanced (dev-master master)    Downloading: 100%Created project in advancedLoading composer repositories with package informationInstalling dependencies (including require-dev)  - Installing ezyang/htmlpurifier (v4.6.0)    Downloading: 100%  - Installing swiftmailer/swiftmailer (dev-master 1471619)    Downloading: 100%  - Installing yiisoft/jquery (2.1.1)    Downloading: 100%  - Installing yiisoft/jquery-pjax (dev-master 520f8c1)    Downloading: 100%  - Installing cebe/markdown (0.9.x-dev 6a3c373)    Downloading: 100%  - Installing yiisoft/yii2-composer (dev-master e5614b1)    Downloading: 100%  - Installing yiisoft/yii2 (dev-master e8e1e41)    Downloading: 100%  - Installing phpspec/php-diff (dev-master 30e103d)    Downloading: 100%  - Installing yiisoft/yii2-swiftmailer (dev-master afd6982)    Downloading: 100%  - Installing yiisoft/yii2-codeception (dev-master 397b494)    Downloading: 100%  - Installing twbs/bootstrap (dev-master 36483af)    Downloading: 100%  - Installing yiisoft/yii2-bootstrap (dev-master b74f3b0)    Downloading: 100%  - Installing yiisoft/yii2-debug (dev-master 403ca66)    Downloading: 100%  - Installing yiisoft/yii2-gii (dev-master c50b66b)    Downloading: 100%Writing lock fileGenerating autoload filesSetting writable: backend/runtime ...doneSetting writable: backend/web/assets ...doneSetting writable: frontend/runtime ...doneSetting writable: frontend/web/assets ...doneE:\>

至此, Yii2 Advanced 模板框架已经安装完毕

Init Yii2 Project

进入到 /path/to/yii-application , 在 cmd 命令下执行 init 命令

E:\yii2advanced>initYii Application Initialization Tool v1.0Which environment do you want the application to be initialized in?  [0] Development  [1] Production  Your choice [0-1, or "q" to quit] 0  Initialize the application under 'Development' environment? [yes|no] yes  Start initialization ...   generate backend/config/main-local.php   generate backend/config/params-local.php   generate backend/web/index-test.php   generate backend/web/index.php   generate common/config/main-local.php   generate common/config/params-local.php   generate console/config/main-local.php   generate console/config/params-local.php   generate frontend/config/main-local.php   generate frontend/config/params-local.php   generate frontend/web/index-test.php   generate frontend/web/index.php   generate yii   generate cookie validation key in backend/config/main-local.php   generate cookie validation key in frontend/config/main-local.php      chmod 0777 backend/runtime      chmod 0777 backend/web/assets      chmod 0777 frontend/runtime      chmod 0777 frontend/web/assets      chmod 0755 yii  ... initialization completed.
Config your Apache2 Web Server
<VirtualHost *:80>  DocumentRoot "E:/yii2advanced/backend/web"  DirectoryIndex index.php  ServerName yii2back </VirtualHost><Directory "E:/yii2advanced/backend/web">    Options Indexes FollowSymLinks    AllowOverride All    Order allow,deny    Allow from all</Directory><VirtualHost *:80>  DocumentRoot "E:/yii2advanced/frontend/web"  DirectoryIndex index.php  ServerName yii2front </VirtualHost><Directory "E:/yii2advanced/frontend/web">    Options Indexes FollowSymLinks    AllowOverride All    Order allow,deny    Allow from all</Directory>

also config your windows hosts

127.0.0.1 yii2front127.0.0.1 yii2back
Migrate DB

现在已经可以访问 http://yii2front or http://yii2back 来访问项目了

不错, Yii2 已经默认开启了 Bootstrap 界面,很清爽

此时我们还没有配置 DB, 当然没有办法 Login or SignUp

首先当然是要建立一个 database , 并且 adjust the components['db'] configuration in common/config/main-local.php accordingly.

再来migrate

进入到 /path/to/yii-application , 在 cmd 命令下执行 migrate 命令

yii migrateE:\yii2advanced>yii migrateYii Migration Tool (based on Yii v2.0.0-dev)Creating migration history table "migration"...done.Total 1 new migration to be applied:    m130524_201442_initApply the above migration? (yes|no) [no]:yes*** applying m130524_201442_init    > create table {{%user}} ... done (time: 0.078s)*** applied m130524_201442_init (time: 0.124s)Migrated up successfully.
SignUp a user

登录后台或者前台之前, 先去 前台注册一个用户, 再登录

Official Docs

Yii2 完全托管在 GitHub 上, 因此文档的翻译工作貌似会很不错, 可惜现在只是发布了很少一部分

https://github.com/yiisoft/yii2/tree/master/docs/guide-zh-CN

期待快点更新

后来又发现一个仓库更新稍快些

https://github.com/yii2-chinesization/yii2-zh-cn/blob/master/guide-zh-CN/README.md

当然英文的文档已经很全了,可以直接看 官方的 Guide

Install Yii2 app via Composer
E:\>composer  create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic yii2app

yii2 的 basic 模板 yii2app 不需要 init 项目, 默认的 login 用户为 demo 和 admin