Home  >  Article  >  Backend Development  >  Start the yaf journey_PHP tutorial

Start the yaf journey_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:14:471255browse

Directory structure

+ 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>

Entry file

The entry file is the entry point for all requests. Generally, all requests are redirected to this entry file with the help of rewrite rules.

A classic entry file 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();

Rewrite rules

Unless we use query string-based routing protocols (Yaf_Route_Simple, Yaf_Route_Supervar), we need to use the Rewrite rules provided by WebServer to direct all requests for this application to the entry file mentioned above.

Modify .htaccess file

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>

Configuration file

In Yaf, the configuration file supports inheritance and sectioning. It also supports PHP constants. You don’t have to worry about parsing performance problems caused by too large a configuration file, because Yaf will load the configuration file when it is first run. , keep the formatted content in memory. It will not be loaded again until the configuration file is modified.

A simple configuration fileapplication/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>

Controller

In Yaf, the default modules/controllers/actions are named after Index. Of course, this can be modified through the configuration file.

For the default module, the controller directory is in the controllers directory under the application directory, and the naming rule for Action is "name + Action"

Default Controllerapplication/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>?>

View file

Yaf supports simple view engines, and supports users to customize their own view engines, such as Smarty.

For the default module, the path of the view file is in the directory with the action name in lowercase in the views directory under the application directory.

A view of a default Actionapplication/views/index/index.phtml

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

Then enter the servername set in nginx.conf in the browser,

Table 4.2. Yaf optional configuration items

名称 值类型 默认值 说明
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 入口文件 入口文件...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn