Heim  >  Artikel  >  Backend-Entwicklung  >  开始yaf之旅_PHP教程

开始yaf之旅_PHP教程

WBOY
WBOYOriginal
2016-07-20 11:14:471206Durchsuche

目录结构

+ public  <span //</span><span 网站根目录</span>
   - index.php <span //</span><span 入口文件</span>
   - .htaccess <span //</span><span 重写规则    </span>
+<span  conf
  </span>|- application.ini <span //</span><span 配置文件   </span>
application/
  +<span  controllers
     </span>- Index.php <span //</span><span 默认控制器</span>
  +<span  views    
     </span>|+ index   <span //</span><span 控制器</span>
        - index.phtml <span //</span><span 默认视图</span>
  + modules <span //</span><span 其他模块</span>
  - library    <span //</span><span 组件目录</span>
  - models  <span //</span><span model目录</span>
  - plugins <span //</span><span 插件目录</span>

入口文件

入口文件是所有请求的入口, 一般都借助于rewrite规则, 把所有的请求都重定向到这个入口文件.

一个经典的入口文件public/index.php

<?<span php
</span><span define</span>("APP_PATH",  <span realpath</span>(<span dirname</span>(<span __FILE__</span>) . '/../')); <span /*</span><span  指向public的上一级 </span><span */</span>
<span $app</span>  = <span new</span> Yaf_Application(APP_PATH . "/conf/application.ini"<span );
</span><span $app</span>->run();

重写规则

除非我们使用基于query string的路由协议(Yaf_Route_Simple, Yaf_Route_Supervar), 否则我们就需要使用WebServer提供的Rewrite规则, 把所有这个应用的请求, 都定向到上面提到的入口文件.

修改.htaccess文件

Nginx的Rewrite (nginx.conf)

<span server {
  listen </span><span 80</span><span ;
  server_name  yaf.demo.com;
  root   document_root;
  index  index.php index.html index.htm;

  </span><span if</span> (!-<span e $request_filename) {
    rewrite </span>^/(.*)  /index.php/$<span 1</span><span  last;
  }
}</span>

配置文件

在Yaf中, 配置文件支持继承, 支持分节. 并对PHP的常量进行支持. 你不用担心配置文件太大造成解析性能问题, 因为Yaf会在第一个运行的时候载入配置文件, 把格式化后的内容保持在内存中. 直到配置文件有了修改, 才会再次载入.

一个简单的配置文件application/conf/application.ini

<span [common]
application.directory </span>= APP_PATH  <span "</span><span /application</span><span "</span><span 
application.dispatcher.catchException </span>= <span 0</span><span 
application.dispatcher.throwException </span>= <span 0</span><span 
application.view.ext </span>= <span '</span><span phtml</span><span '</span><span 
[product : common]
;enable the error controller
application.dispatcher.catchException</span>=<span 1</span>

控制器

在Yaf中, 默认的模块/控制器/动作, 都是以Index命名的, 当然,这是可通过配置文件修改的.

对于默认模块, 控制器的目录是在application目录下的controllers目录下, Action的命名规则是"名字+Action"

默认控制器application/controllers/Index.php

 
<?<span php
</span><span class</span> IndexController <span extends</span><span  Yaf_Controller_Abstract {
   </span><span public</span> <span function</span> indexAction() {<span //</span><span 默认Action</span>
       <span $this</span>->getView()->assign("content", "Hello World"<span );
   }
}
</span>?>

视图文件

Yaf支持简单的视图引擎, 并且支持用户自定义自己的视图引擎, 比如Smarty.

对于默认模块, 视图文件的路径是在application目录下的views目录中以小写的action名的目录中.

一个默认Action的视图application/views/index/index.phtml

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

然后在浏览器输入nginx.conf设置的servername,

 

表 4.2. Yaf可选配置项

名称 值类型 默认值 说明
application.ext String php PHP脚本的扩展名
application.bootstrap String Bootstrapplication.php Bootstrap路径(绝对路径)
application.library String application.directory + "/library" 本地(自身)类库的绝对目录地址
application.baseUri String NULL 在路由中, 需要忽略的路径前缀, 一般不需要设置, Yaf会自动判断.
application.dispatcher.defaultModule String index 默认的模块
application.dispatcher.throwException Bool True 在出错的时候, 是否抛出异常
application.dispatcher.catchException Bool False 是否使用默认的异常捕获Controller, 如果开启, 在有未捕获的异常的时候, 控制权会交给ErrorController的errorAction方法, 可以通过$request->getException()获得此异常对象
application.dispatcher.defaultController String index 默认的控制器
application.dispatcher.defaultAction String index 默认的动作
application.view.ext String phtml 视图模板扩展名
application.modules String Index 声明存在的模块名, 请注意, 如果你要定义这个值, 一定要定义Index Module
application.system.* String * 通过这个属性, 可以修改yaf的runtime configure, 比如application.system.lowcase_path, 但是请注意只有PHP_INI_ALL的配置项才可以在这里被修改, 此选项从2.2.0开始引入

 

<em id="__mceDel"><em id="__mceDel"><span <br /><br /></span></em></em>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440265.htmlTechArticle目录结构 + public - index.php - .htaccess + |- application.ini application/ + - Index.php + |+ index - index.phtml + modules - library - models - plugins 入口文件 入口文件...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn