• PHP框架Yii系列教程(一):入门实例


    1准备Yii源码

    首先新建helloyii目录作为Web应用的根目录,并添加到Nginx的配置文件中。然后将Yii框架源码部署到helloyii下,目录结构如下:

    helloyii/

    |-- framework

        |-- ……

        |-- YiiBase.php

        |-- yiic

        |-- yii.php

        `-- zii

    2编写HelloWorld

    2.1目录结构

    程序目录结构如下:

    app/

    |-- index.php

    `-- protected

        |-- controllers

        |   `-- HelloController.php

        `-- views

            `-- hello

                `-- result.php

    2.2主要代码

    index.php

    ===============================================================================

    <?php

           // change the following paths if necessary

           $yii=dirname(__FILE__).'/../framework/yii.php';

           // remove the following lines when in production mode

           defined('YII_DEBUG') or define('YII_DEBUG',true);

           // specify how many levels of call stack should be shown in each logmessage

           defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

           require_once($yii);

           Yii::createWebApplication()->run();

    ?>

    protected/controllers/HelloController.php

    ===============================================================================

    <?php

           class HelloController extends CController

           {

                    public function actionSay()

                    {

                            $varYii = "hi,yii";

                           $this->render('result', array('varYii'=>$varYii));

                    }

           }

    ?>

    protected/views/hello/result.php

    ===============================================================================

    <?php

           echo $varYii;

    ?>

    2.3开始访问

    现在可以直接访问了:http://helloyii.com/app/index.php?r=hello/say

    3源码解析

    3.1资源映射规则

    访问Url经过index.php处理,将请求转发到HelloController的actionSay方法中,然后通过result.php生成最终HTML页面。具体映射关系如下图所示:


    注:如果ControllerIDActionID为默认值siteindex的话,则可以通过http://helloyii.com/app/index.php直接访问。请求会转发给SiteControlleractionIndex()方法。

    4自动生成代码工具

    Yii提供了Yiic和Gii两个代码生成工具,可以生成内容更加丰富的实例,下面就来试用一下。

    首先切换到/export/data/helloyii,然后执行:

    framework/yiicwebapp demo

    将会在/export/data/helloyii/demo中自动生成示例程序代码。

    现在在浏览器中访问helloyii.com/demo/index.php即可看到成功页面。


    5常见问题

    访问index.php时,PHP打印警告日志:Warning: date(): It is not safe to rely on the system's timezonesettings. You are *required* to use the date.timezone setting or thedate_default_timezone_set() function…

    在php.ini中设置默认时区,或者修改helloyii/demo/protected/views/layouts/main.php:

    ===============================================================================

    ……

    <?phpdate_default_timezone_set('Europe/Athens'); ?>

           <div id="footer">

                    Copyright &copy; <?phpecho date('Y'); ?> by My Company.<br/>

                    All Rights Reserved.<br/>

                    <?php echo Yii::powered();?>

           </div><!-- footer -->

    ===============================================================================

    参考资料

    1一起学Yii—-Hello world

    http://istrone.com/?p=451

    2 yii框架之hello world

    http://513394217.blog.163.com/blog/static/10979118620124288721586/

    3 Creating Your First Yii Application

    http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app

    4应用Yii1.1和PHP5进行敏捷Web开发

    http://yiibook.com/book/agile_web_application_development_with_yii1.1_and_php5/chapter-2

  • 相关阅读:
    位运算符设置权限
    urlencode、urldecode、rawurlencode、rawurldecod
    二分查找法的mid值 整数溢出问题
    GIT 常用命令
    nginx配置反向代理转发
    PHP实现无限极分类
    PHP面试题目整理(持续更新)
    去除input的默认样式
    git 常用指令
    数组去重
  • 原文地址:https://www.cnblogs.com/xiaomaohai/p/6157746.html
Copyright © 2020-2023  润新知