適合初學者對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/,看到以下內容,說明就成功了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM