适合初学者对Yaf框架的学习(二)

前言

上一篇介绍了Yaf的安装,适合初学者对Yaf框架的学习(一)http://www.cnblogs.com/joshua317/articles/4622551.html,这篇来介绍一下Yaf的布局

一、Yaf的目录结构

 1 YafWeb
 2     index.php #入口文件
 3     application #应用目录
 4         Bootstrap.php
 5         controllers #控制器目录
 6             Index.php #默认Index控制器
 7         library #本地类库
 8         modules #其他模块
 9         models  #model目录
10         plugins #插件目录
11         views #视图目录
12             index #和Index控制器文件相对应目录
13                 index.phtml #具体的index模板文件,后缀可以自己在配置文件中设置
14     conf #配置文件目录
15         app.ini #具体的配置文件
16     public #公共资源目录

 

二、创建nginx配置文件yafweb.conf

server {
        listen       9880;#监听的端口号,这个由你自己来定
        server_name  101.200.***.***;#这个是我主机的ip,隐藏了后两个
        index index.html index.htm index.php;
        root /alidata/www/YafWeb/;

        location ~ .*\.(php|php5)?$
        {
                #fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }
        #伪静态规则
        location /{
                if (!-e $request_filename) {
                    rewrite ^/(.*)/index.php?$1 last;
                }
        }
        access_log  /alidata/log/nginx/access/yafweb.log;
}

然后执行以下任意一条命令 重启或者平滑重启nginx

/etc/init.d/nginx reload
#或者
/etc/init.d/nginx restart

 

三、各文件的内容

1.入口文件index.php

<?php
define('APP_ROOT', realpath(dirname(__FILE__)));
define("APP_PATH", APP_ROOT . '/application');
define("APP_CONFIG", APP_ROOT.'/conf');

//定义全局library
ini_set('yaf.library', APP_PATH.'/library');

//第二个参数用来区分开发环境、测试环境、生产环境配置 对应config中内容
//实例化Bootstrap, 依次调用Bootstrap中所有_init开头的方法
$app  = new Yaf_Application( APP_CONFIG."/app.ini",'common');

$app->bootstrap()->run();

2.配置文件app.ini

[common]
application.modules= Index,Api
;layout
application.directory = APP_PATH
application.bootstrap = APP_PATH "/Bootstrap.php"
application.library = APP_PATH "/library"
application.layoutpath = APP_PATH "/view/"
application.cache_config = 0
application.view.ext = "phtml"

;app application
.baseUri = '' ;not used application.dispatcher.defaultModule = index application.dispatcher.defaultController = index application.dispatcher.defaultAction = index
;database config 数据库配置 database
.config.charset = "utf8" database.config.host = localhost database.config.name = "mysql" database.config.user = "myuser" database.config.pwd = "mypwd" database.config.port = "3306" ;开发环境 [develop : common] ;errors (see Bootstrap::initErrors) application.showErrors = 1 application.throwException = 1 ;生产环境 [product : common] ;errors (see Bootstrap::initErrors) application.showErrors = 0 application.throwException = 0

说明:new Yaf_Application( APP_CONFIG."/app.ini",'common'); yaf读取配置文件app.ini,这样就会创建了一个对象

object(Yaf_Application)#1 (7) {
  ["config":protected]=>
  object(Yaf_Config_Ini)#2 (2) {
    ["_config":protected]=>
    array(2) {
      ["application"]=>
      array(8) {
        ["modules"]=>
        string(9) "Index,Api"
        ["directory"]=>
        string(31) "/alidata/www/YafWeb/application"
        ["bootstrap"]=>
        string(45) "/alidata/www/YafWeb/application/Bootstrap.php"
        ["library"]=>
        string(39) "/alidata/www/YafWeb/application/library"
        ["cache_config"]=>
        string(1) "0"
        ["view"]=>
        array(1) {
          ["ext"]=>
          string(5) "phtml"
        }
        ["baseUri"]=>
        string(0) ""
        ["dispatcher"]=>
        array(3) {
          ["defaultModule"]=>
          string(5) "index"
          ["defaultController"]=>
          string(5) "index"
          ["defaultAction"]=>
          string(5) "index"
        }
      }
      ["database"]=>
      array(1) {
        ["config"]=>
        array(6) {
          ["charset"]=>
          string(4) "utf8"
          ["host"]=>
          string(9) "localhost"
          ["name"]=>
          string(5) "mysql"
          ["user"]=>
          string(4) "root"
          ["pwd"]=>
          string(10) "aa60dc2991"
          ["port"]=>
          string(4) "3306"
        }
      }
    }
    ["_readonly":protected]=>
    bool(true)
  }
  ["dispatcher":protected]=>
  object(Yaf_Dispatcher)#4 (10) {
    ["_router":protected]=>
    object(Yaf_Router)#5 (2) {
      ["_routes":protected]=>
      array(1) {
        ["_default"]=>
        object(Yaf_Route_Static)#6 (0) {
        }
      }
      ["_current":protected]=>
      NULL
    }
    ["_view":protected]=>
    NULL
    ["_request":protected]=>
    object(Yaf_Request_Http)#3 (11) {
      ["module"]=>
      NULL
      ["controller"]=>
      NULL
      ["action"]=>
      NULL
      ["method"]=>
      string(3) "GET"
      ["params":protected]=>
      array(0) {
      }
      ["language":protected]=>
      NULL
      ["_exception":protected]=>
      NULL
      ["_base_uri":protected]=>
      string(0) ""
      ["uri":protected]=>
      string(1) "/"
      ["dispatched":protected]=>
      bool(false)
      ["routed":protected]=>
      bool(false)
    }
    ["_plugins":protected]=>
    array(0) {
    }
    ["_auto_render":protected]=>
    bool(true)
    ["_return_response":protected]=>
    bool(false)
    ["_instantly_flush":protected]=>
    bool(false)
    ["_default_module":protected]=>
    string(5) "Index"
    ["_default_controller":protected]=>
    string(5) "Index"
    ["_default_action":protected]=>
    string(5) "index"
  }
  ["_modules":protected]=>
  array(2) {
    [0]=>
    string(5) "Index"
    [1]=>
    string(3) "Api"
  }
  ["_running":protected]=>
  bool(false)
  ["_environ":protected]=>
  string(7) "product"
  ["_err_no":protected]=>
  int(0)
  ["_err_msg":protected]=>
  string(0) ""
}

3.Bootstrap.php文件

<?php
class Bootstrap extends Yaf_Bootstrap_Abstract {
    #里面具体需要啥,自己补充
}

4.Base.php文件

<?php
class BaseController extends Yaf_Controller_Abstract{

    public function init()
    {

    }
}

?>

5.Index控制器文件

<?php
class IndexController extends BaseController{

    public function indexAction()
    {
        $this->getView()->assign("content", "Hello World");
    }

}
?>

6.index.phtml文件

<html>
 <head>
   <title>Hello World</title>
 </head>
 <body>
  <?php echo $content;?>
 </body>
</html>

 

四、访问地址 http://101.200.***.***:9890/,看到以下内容,说明就成功了

 

posted @ 2015-07-05 16:44  joshua317  阅读(7710)  评论(0编辑  收藏  举报