Yaf基本学习使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Pencil1312/article/details/79994487

1、初始化yaf配置:

在php扩展目录加入yaf扩展文件后,配置php.ini文件,添加如下代码:

;添加yaf扩展
extension=php_yaf.dll

;设置自动加载机制
yaf.use_spl_autoload=1 

;启用命名空间
yaf.use_namespace=1

2、最基本的yaf目录结构:

+ public
  |- index.php //入口文件
  |- .htaccess //重写规则    
  |+ css
  |+ img
  |+ js
+ conf
  |- application.ini //配置文件   
+ application
  |+ controllers
     |- Index.php //默认控制器
  |+ views    
     |+ index   //控制器
        |- index.phtml //默认视图
  |+ modules //其他模块
  |+ library //本地类库
  |+ models  //model目录
  |+ plugins //插件目录

3、入口文件内容:

<?php
//指向public的上一级 
define("APP_PATH",  realpath(dirname(__FILE__) . '/../')); 

//加载框架的配置文件
$app = new Yaf\Application(APP_PATH . "/conf/application.ini");

//加载bootstrap配置内容启动
$app->bootstrap()->run();

4、.htacess文件内容如下:

该文件实现对路由的重写功能,每个url请求都会经过index.php入口文件。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php

5、application.ini配置文件默认内容:

此文件用于定义自己的常量,具体使用接下来用到会进行说明。

[yaf]
;APP_PATH is the constant defined in index.php
application.directory=APP_PATH "/app"
application.ext="php"
application.view.ext="phtml"
application.modules="Index,Admin"
application.library=APP_PATH "/lib"
application.library.directory=APP_PATH "/lib"
application.library.namespace=""    
application.bootstrap=APP_PATH "/app" "/Bootstrap.php"
application.baseUri=""
application.dispatcher.defaultRoute=""
application.dispatcher.throwException=1
application.dispatcher.catchException=1
application.dispatcher.defaultModule="index"
application.dispatcher.defaultController="index"
application.dispatcher.defaultAction="index"
;custom settings
application.layout.directory=APP_PATH "/app" "/views" "/layouts"
application.protect_from_csrf=1
application.encoding=UTF-8
;product section inherit from yaf section
[product:yaf]
; user configuartions list here
database.mysql.host=localhost
database.mysql.port=3306
database.mysql.user=
database.mysql.password=
database.mysql.database=
database.mysql.charset=utf8

6、bootstrap文件内容:

实际的初始化方法按照自己的实际需要进行添加:

<?php
use Yaf\Bootstrap_Abstract;
use Yaf\Dispatcher;
/**
 * 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用,
 * 这些方法, 都接受一个参数:Yaf_Dispatcher $dispatcher
 * 调用的次序, 和申明的次序相同
 */

class Bootstrap extends Bootstrap_Abstract {

    //加载应用初始化配置
    public function _initConfig() {
        $config = Yaf\Application::app()->getConfig();
        Yaf\Registry::set("config", $config);
    }

    //定义应用默认模块和默认的控制器及方法
    public function _initDefaultName(Dispatcher $dispatcher) {
        $dispatcher->setDefaultModule("Index")->setDefaultController("index")->setDefaultAction("index");
    }

    //初始化应用的总的路由配置
    public function _initRoute(Dispatcher $dispatcher)
    {
        $config = new Yaf\Config\Ini(APP_PATH . '/conf/routing.ini');
        $dispatcher->getRouter()->addConfig($config);
    }

    //初始化模块自己专属的配置
    public function _initModules(Yaf\Dispatcher $dispatcher)
    {
        $app = $dispatcher->getApplication();

        $modules = $app->getModules();
        foreach ($modules as $module) {
            if ('index' == strtolower($module)) continue;

            require_once $app->getAppDirectory() . "/modules" . "/$module" . "/_init.php";
        }
    }
}

相应的,模块新增方法如下:

//往conf/application.ini文件中加入下列代码
application.modules="index,admin,test"

这里表明应用采用index模块,admin模块和test模块。相应的,需要添加对应的目录如下:

+ public
  |- index.php //入口文件
  |- .htaccess //重写规则    
  |+ css
  |+ img
  |+ js
+ conf
  |- application.ini //配置文件   
+ application
  |+ controllers
     |+Backend
        |-Index.php //控制器文件
     |- Index.php //默认控制器
  |+ views    
     |+ index   //控制器
        |- index.phtml //默认视图
  |+ modules //其他模块
     |+Admin
        |+config
            |-routes.ini //模块路由规则
        |+controller //模块控制器文件目录
            |-Index.php
            |-Test.php
        |+views //模块视图文件目录
        |-_init.php  //加载模块的路由规则
     |+Test
        |+config
            |-routes.ini
        |+controller
        |+views
        |-_init.php
  |+ library //本地类库
  |+ models  //model目录
  |+ plugins //插件目录

6、路由配置:

配置默认模块路由:

;默认模块中的backend目录的路由配置
backend_index.type="rewrite"
backend_index.match="/(backend|backend/)$"
backend_index.route.module="index"
backend_index.route.controller="backend_index"
backend_index.route.action="index"
backend_post_index.type="rewrite"
backend_post_index.match="/Backend/(posts|posts/)$"
backend_post_index.route.module="index"
backend_post_index.route.controller="backend_posts"
backend_post_index.route.action="index"

此时可以通过访问下面的url,访问index模块的backend目录下的Index.php的Backend_IndexController控制器中的index方法(默认方法)。

http://www.yaftest.io/backend/

错误示范:刚开始把backend_index这个控制器改为index,结果每次都会跑index默认模块目录下的index控制器而不是backend目录下的index控制器。原因在于这些控制器都没有使用命名空间,因此,改成index时,只能被判断成是访问前者。

配置其他模块路由:

//第一步:编辑routes.ini文件,配置路由规则
;Admin routes
admin.admin_index.type="rewrite"
admin.admin_index.route.module="admin"

//此处可以匹配到admin模块中的Index控制器
admin.admin_index.match="/(admin|admin/)$"
admin.admin_index.route.controller="index"
admin.admin_index.route.action="index"

//此处可以匹配到admin模块中的Test控制器
//错误写法:admin.admin_index.match="/admin/test"(无法匹配到test控制器里面的方法)
admin.admin_index.match="/(admin/test/)$"
admin.admin_index.route.controller="test"
admin.admin_index.route.action="index"

//第二步:配置_init.php文件,将当前模块的路由规则加入应用生效
<?php
$dis=Yaf\Dispatcher::getInstance();

//Initialize Routes for Admin module
$routes = new Yaf\Config\Ini(__DIR__ . "/config" . "/routes.ini");
$dis->getRouter()->addConfig($routes->admin);

此时可以通过访问下面的url,访问test控制器中的test方法,其中后一个test为控制器的方法名,直接通过修改即可更改对方法的访问。

http://www.yaftest.io/admin/test/test

7、控制器简单模板:

编辑application/controllers/backend/index.php文件:

<?php
use Yaf\Controller_Abstract;
use Yaf\Dispatcher;
class Backend_IndexController extends Controller_Abstract
{
    public function indexAction()
    {//默认Action
        $this->getView()->assign("content", "I am in application/controllers/Backend/Index/indexAction");
    }

    public function testAction(){
//        $this->getView()->assign("testcontent", "test hello");
        Dispatcher::getInstance()->disableView(0);
        echo 'Great,It Works!';
    }
}

8、视图文件模板:

默认模块的视图文件位于application/views下的对应文件目录中,而其他模块对应的视图文件位于模块各自的views文件目录中。

<html>
<head>
    <title>My first yaf app</title>
</head>
<body>
<?php echo $content;?>
</body>
</html>

以上是本人对yaf初次学习的学习心得记录,只为备忘使用,如果阅读过程中发现文中问题,期待您的留言。

猜你喜欢

转载自blog.csdn.net/Pencil1312/article/details/79994487
今日推荐