yaf 項(xiàng)目目錄結(jié)構(gòu)
+ public
|- index.php // 入口文件
|- .htaccess // 重寫規(guī)則
|- composer.json // composer 配置文件
|+ static
|- css
|- img
|- js
+ conf
|- application.ini // 項(xiàng)目配置文件
+ application
|+ controllers
|- Index.php // 默認(rèn)控制器
|+ views
|+ index // 控制器
|- index.phtml // 默認(rèn)視圖
|+ modules // 其他模塊
|+ library // 本地類庫
|+ models // model目錄
|+ plugins // 插件目錄
入口文件:public/index.php
<?php
// 定義項(xiàng)目根目錄:public 目錄的上一級(jí)目錄
define("APP_PATH", realpath(dirname(__FILE__).'/../'));
$app = new Yaf_Application(APP_PATH."/conf/application.ini");
$app->bootstrap()->run();
apache 重寫規(guī)則文件:public/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
apache 虛擬主機(jī)配置
- 項(xiàng)目根目錄路徑為:E:/Project/hyso/yaf,Directory 配置項(xiàng)為項(xiàng)目根目錄下的 public 目錄(index.php 文件所在目錄)
- 虛擬主機(jī)域名:local-yaf.bmtrip.com,本地 hosts 文件相應(yīng)加上此域名的配置
<VirtualHost *:8080>
ServerAdmin tobehyso@163.com
DocumentRoot "E:/Project/hyso/yaf/public"
ServerName local-yaf.bmtrip.com
<Directory "E:/Project/hyso/yaf/public">
Options FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
ErrorLog "logs/local-yaf.bmtrip.com-error.log"
CustomLog "logs/local-yaf.bmtrip.com-access.log" common
</VirtualHost>
composer 配置文件:public/composer.json
{
"config": {
"vendor-dir": "../vendor"
}
}
處理 composer 依賴關(guān)系
運(yùn)行命令行窗口,切換到項(xiàng)目根目錄,執(zhí)行以下命令:
composer install
默認(rèn)控制器文件:application/controllers/Index.php
<?php
class IndexController extends Yaf_Controller_Abstract
{
/**
* 默認(rèn)Action
*
*/
public function indexAction()
{
$this->getView()->assign("content", "Hello World");
}
/**
* test Action
*
*/
public function testAction()
{
$this->getView()->assign("content", "test");
$this->getView()->display('index/index.phtml');
return false;
}
}
項(xiàng)目配置文件:conf/application.ini
[common]
application.directory = APP_PATH "/application/"
application.bootstrap = APP_PATH "/application/Bootstrap.php"
[product:common]
Bootstrap 文件:application/Bootstrap.php
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract
{
public function _initConfig()
{
$config = Yaf_Application::app()->getConfig();
Yaf_Registry::set("config", $config);
}
public function _initDefaultUrl(Yaf_Dispatcher $dispatcher)
{
$dispatcher
->setDefaultModule("Index")
->setDefaultController("Index")
->setDefaultAction("test");
}
public function _initAutoload()
{
require '../vendor/autoload.php';
}
}
默認(rèn)視圖文件:application/views/index/index.phtml
- 視圖文件目錄:application/views
- 默認(rèn)控制器視圖文件目錄(目錄名與控制器文件名保持一致):application/views/index
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php echo $content;?>
</body>
</html>
瀏覽器中訪問控制器