PHP 8.5.10 Released!

快速入门

示例 #1 一个典型的应用目录结构

- .htaccess           // 重写规则
+ public/
  | - index.php       // 应用入口
  | + css/
  | + js/
  | + img/
+ conf/
  | - application.ini // 应用配置
- application/
  - Bootstrap.php     // 引导
  + controllers/
     - Index.php      // 默认控制器
  + views/
     |+ index/
        - index.phtml // 默认动作的视图模板
  + library/          // 类库
  + models/           // 模型
  + plugins/          // 插件

将 web 服务器的 DocumentRoot 设置为 public 目录,这样从 web 就只能访问公开资源。

示例 #2 多模块应用的目录结构

+ public/
+ conf/
+ application/
  + modules/
    + Index/       // 默认模块
      + controllers/
      + views/
    + Admin/       // 另一个模块
      + controllers/
      + views/
  + library/
  + models/
  + plugins/
  - Bootstrap.php

在多模块应用中,每个模块在 application/modules/<ModuleName>/ 下都有自己的 controllersviews 目录。 已注册的模块在 application.modules 中声明。

示例 #3 入口

public 目录下的 index.php 是整个应用的唯一入口, 应该把所有请求都重写到它(在 Apache + mod_php 下可以使用 .htaccess,或者你的 web 服务器中的等效配置,见下文)。

<?php
define("APPLICATION_PATH",  dirname(dirname(__FILE__)));

$app  = new Yaf_Application(APPLICATION_PATH . "/conf/application.ini");
$app->bootstrap() //call bootstrap methods defined in Bootstrap.php
 ->run();
?>

示例 #4 重写规则

#for apache (.htaccess)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php

#for nginx
server {
  listen 80;
  server_name domain.com;
  root   document_root;
  index  index.php index.html index.htm;

  location / {
    try_files $uri $uri/ /index.php?$args;
  }
}

#for lighttpd
$HTTP["host"] =~ "(www.)?domain.com$" {
  url.rewrite = (
     "^/(.+)/?$"  => "/index.php/$1",
  )
}

示例 #5 引导

Bootstrap 类中所有名称以 _init 开头的方法都会被 Yaf_Application::bootstrap() 按定义顺序自动调用。每个这样的方法都会收到 Yaf_Dispatcher 实例作为参数。 名称为其他名称的方法不会被自动调用。

<?php
class Bootstrap extends Yaf_Bootstrap_Abstract
{
    public function _initConfig(Yaf_Dispatcher $dispatcher)
    {
        // called first
    }

    public function _initPlugin(Yaf_Dispatcher $dispatcher)
    {
        // called second
    }

    public function _initRoute(Yaf_Dispatcher $dispatcher)
    {
        // called third
    }
}
?>

示例 #6 应用配置

application.ini 是应用的配置文件。 其中可以使用 index.php 中定义的常量, 并且各个分节之间可以相互继承。

[yaf]
;APPLICATION_PATH is the constant defined in index.php
application.directory=APPLICATION_PATH "/application/"

;product section inherit from yaf section
[product:yaf]
foo=bar

示例 #7 使用 PHP 数组作为应用配置

除了 INI 配置文件,也可以把一个普通的 PHP 数组作为配置传递给 Yaf_Application::__construct()

<?php
$config = array(
    "application" => array(
        "directory" => APPLICATION_PATH . "/application/",
    ),
);

$app = new Yaf_Application($config);
?>

示例 #8 默认控制器

在 Yaf 中,默认控制器名为 IndexController

<?php
class IndexController extends Yaf_Controller_Abstract
{
    // default action name
    public function indexAction()
    {
        $this->getView()->content = "Hello World";
    }
}
?>

示例 #9 默认视图模板

默认控制器/动作的视图脚本是 application/views/index/index.phtml。Yaf 提供了一个 名为 Yaf_View_Simple 的小型视图引擎, 它的模板是普通的 PHP 脚本。

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

示例 #10 运行应用

将浏览器指向已配置的域名 (例如 http://www.example.com),响应将如下所示:

以上示例的输出类似于:

<html>
 <head>
   <title>Hello World</title>
 </head>
 <body>
   hello world
 </body>
</html>

注意:

上面的最小示例也可以使用随 » Yaf 源码仓库 一起发布的 Yaf 代码生成器 (tools/cg/yaf_cg)生成:

$ cd tools/cg
$ ./yaf_cg -d output_directory [-a application_name] [--namespace]

yaf_cg 会生成比本教程所示更完整、带注释的骨架: 一个带重写规则的 public/ 入口、配置了插件和自定义路由的 Bootstrap、示例模型、错误控制器以及视图模板。它是开发实际应用的 一个良好起点。

添加备注

用户贡献的备注

此页面尚无用户贡献的备注。
To Top