Yii2.0.8框架开发环境14步快速搭建(基本应用程序模板)

学习笔记 马富天 2016-05-22 13:39:36 110 0

【摘要】介绍如何快速搭建yii框架环境,适合于使用过框架的朋友们如ThinkPHP框架,了解ThinkPHP框架的朋友,如果第一次接触yii框架,可以参照这篇文章,快速上手。

基本步骤,分为14步:

1.下载yii框架,官网地址:http://www.yiichina.com/download。

2.解压之后,把basic文件夹中的所有文件都放置在本地服务器根目录下,并在/config/web.php中设置cookieValidationKey的值,值可以是任意字符。

3.通过浏览器访问url:http://localhost/web/index.php,若出现yii框架的欢迎界面(Congratulations提示),则说明安装成功。

4.将入口文件移动到根目录下,可修改之后可以直接访问http://localhost/index.php:

修改步骤如下:

(1)把/web/index.php剪切到根目录下

(2)在/index.php文件中:

把 '/../vendor/autoload.php' 改成 './vendor/autoload.php'

把 '/../vendor/yiisoft/yii2/Yii.php' 改成 './vendor/yiisoft/yii2/Yii.php'

把 '/../config/web.php' 改成 './config/web.php'

(3)在/config/web.php的$config数组中的'components'项,即$config['components']里加入子元素:

  1. 'assetManager' => [
  2.      'basePath' => '@webroot/web/assets',
  3.      'baseUrl' => '@web/web/assets'
  4. ],

(4)把/assets/AppAsset.php文件中的

  1. public $css = [
  2.      'css/site.css',
  3. ];

改成:

  1. public $css = [
  2.      '/web/css/site.css',
  3. ];

5.创建"第一次问候",步骤如下:

(1)在/controllers目录下创建一个文件,命名为HelloController.php,并写入下面内容:

  1. namespace app\controllers;
  2. use yii\web\Controller;
  3. class HelloController extends Controller
  4. {
  5.      public function actionSay($message = 'Hello')
  6.     {
  7.          return $this->render('say', ['message' => $message]);
  8.     }
  9. }

(2)在/views下创建一个新文件夹,命名为hello,并在这个文件夹下创建一个文件,命名为say.php,即/views/hello/say.php,并且在say.php中写入如下内容:

  1. use yii\helpers\Html;
  2. echo Html::encode($message);

(3)通过url:http://localhost/index.php?r=hello/say&message=hi或者http://localhost/index.php?r=hello/say即可访问,页面中会输出hi或者hello。

(1)在/config/web.php文件中,找到$config['components']的子元素$config['components']['urlManager'],把它的注释打开

(2)在网站的根目录下,创建一个.htaccess文件,并且写入下面内容:

  1. <IfModule mod_rewrite.c>   
  2.   
  3.   
  4. Options +FollowSymLinks  
  5. IndexIgnore */*  
  6. RewriteEngine on  
  7.   
  8. # if a directory or a file exists, use it directly  
  9. RewriteCond %{REQUEST_FILENAME} !-f  
  10. RewriteCond %{REQUEST_FILENAME} !-d  
  11.   
  12. # otherwise forward it to index.php  
  13. RewriteRule . index.php  
  14.   
  15.   
  16. </IfModule>

(3)添加伪静态.html 在/config/web.php中找到$config['components']['urlManager'],并添加子元素,suffix = '.html',即$config['components']['urlManager']['suffix']='.html' 来实现。

(4)去掉?号,即参数的路径化,在$config['components']['urlManager']['rules']中添加路由规则,

例如,

  1. $config['components']['urlManager']['rules'] = [
  2.     '<controller>/<action>/<message:\w+>'  => '<controller>/<action>',
  3. ]

则,可以再在url中直接输入http://localhost/hello/say/hi.html来访问。

注意,可以添加多个规则如:

  1. '<controller>/<action>/<message:\w+>'  => '<controller>/<action>',
  2. '<controller>/<action>/<id:\d+>'  => '<controller>/<action>',
  3. 'sites'=>'site/index',
  4. '<id:\d+>'=>'article/index',

但是如果有后缀需要补充完整后缀,等等。

7.配置数据库信息,在/config/db.php的文件中,可以设置连接数据库的基本信息,连接主机名,数据库名,用户名和密码等等。

8.可关闭调试模式,在/index.php中把defined('YII_DEBUG') or define('YII_DEBUG', true)改成defined('YII_DEBUG') or define('YII_DEBUG', false)

9.设置默认路由$config['defaultRoute']='hello/say',即设置了默认的控制器和操作。直接访问url:http://localhost/?message=110就是访问http://localhost/hello/say/110.html

10.注意,yii框架模块的控制器是SiteController,默认的方法是index方法,在控制器中可以设置默认的方法,即

把public $defaultAction='login';放在SiteController.php中,注意login是区分大小写的,并且路径要写全。

11.自定义404页面,在配置文件中修改$config['errorHandler']['errorAction']='common/error',并且创建一个新的控制器CommonController在这个控制器中写入一个方法:

  1. class CommonController extends Controller
  2. {	    
  3. 	public function actionError()
  4. 	 {
  5. 		 echo "404";
  6. 	 }
  7. }

这样当页面找不到或者报错的时候会跳到这个控制器的error方法中。

12.不使用yii布局模式,在对应的控制器中,设置成员变量为:

public $layout = false; //不使用布局

13.视图中引入css或js文件,直接在视图中引入,写绝对路径。

14.在控制器的方法中向视图传入变量:return $this->render('say', ['name' => "mafutian",'arr'=>$arr]);意思是使用当前控制器的say模板,并设置了两个变量$name和$arr。

在视图中输入变量:直接使用echo $say即可,当然需要用PHP标签扩起来。

以上就是14个步骤,需要注意的是这是yii2.0.8版本。

版权归 马富天个人博客 所有

本文标题:《Yii2.0.8框架开发环境14步快速搭建(基本应用程序模板)》

本文链接地址:http://www.mafutian.com/122.html

转载请务必注明出处,小生将不胜感激,谢谢! 喜欢本文或觉得本文对您有帮助,请分享给您的朋友 ^_^

2

0

上一篇《 说说CDN加速服务器——内容分发网络 》 下一篇《 Eclipse中...No java virtual machine was found...解决方法 》

暂无评论

评论审核未开启
表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情 表情
验证码

TOP10

  • 浏览最多
  • 评论最多