Yaf基本学习使用

3 篇文章 0 订阅
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初次学习的学习心得记录,只为备忘使用,如果阅读过程中发现文中问题,期待您的留言。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: YAF 队列是基于 PHP 的队列系统,可以通过它来实现后台异步任务的处理。使用方法如下: 1. 首先需要在 PHP 中安装 YAF 扩展。 2. 然后可以在你的 PHP 代码中使用 Yaf_Queue 类进行队列的操作。 3. 可以使用 Yaf_Queue 类的 push 方法向队列中添加元素,pop 方法从队列中取出元素。 4. 可以使用 Yaf_Queue 类的 isEmpty 方法来判断队列是否为空。 5. 也可以使用 Yaf_Queue 类的 getCount 方法来获取队列中元素的个数。 6. 队列中可以存储任何类型的元素。 具体示例请参考YAF官方文档. ### 回答2: Yaf队列是Yaf框架提供的一种数据存储机制,用于在应用程序中处理任务队列。以下是关于Yaf队列的使用方法: 1. 创建队列:在Yaf框架中,可以使用Yaf\Loader类的registerLocalNamespace方法注册队列命名空间。例如,可以在Bootstrap类的_initLoader方法中注册:“Yaf\Loader::getInstance()->registerLocalNamespace('Queue')”。这将帮助自动加载队列类。 2. 定义任务类:创建一个任务类,例如“Queue\MyTask”,该类将实现Yaf队列接口。这个类需要实现push方法用于添加任务,以及pop方法用于获取任务。在任务类中,可以定义处理任务的具体逻辑。 3. 添加任务:在应用程序的逻辑中,创建一个任务实例并调用push方法将任务添加到队列中。例如,“$task = new Queue\MyTask(); $task->push('task data');”。任务数据可以是任何需要处理的数据。 4. 处理任务:在队列的消费者服务中,调用pop方法从队列中获取任务,并进行处理。例如,“$task = new Queue\MyTask(); $data = $task->pop(); $task->process($data);”。这将处理队列中的下一个任务。 5. 定时处理任务:可以使用定时任务工具,例如Linux的Cron或Windows的计划任务,来定时地调用处理任务的代码。这样,可以按照需要处理任务队列中的任务。 总的来说,使用Yaf队列需要创建任务类并实现队列接口的方法,然后在应用程序中添加任务到队列,最后使用消费者服务或定时任务来处理队列中的任务。这样可以实现高效的任务处理机制。 ### 回答3: yaf(Yet Another Framework)是一个快速、简单、高效的PHP框架,它提供了很多功能和组件,其中包括队列。 Yaf队列的使用方式如下: 1. 首先,需要使用Composer安装yaf队列库。可以在项目根目录中创建一个composer.json文件,并添加以下内容: ```json { "require": { "monolog/monolog": "^2.0", "yaf/queue": "^2.0" } } ``` 然后在命令行中运行 `composer install` 进行安装。 2. 在代码中使用YafQueue类创建一个队列实例。可以指定队列的驱动(如Redis、Beanstalkd等)和一些配置选项。例如: ```php use Yaf\Queue\QueueFactory; $queue = QueueFactory::create('redis', [ 'host' => '127.0.0.1', 'port' => 6379, 'database' => 0, ]); ``` 3. 使用队列的 `push` 方法将任务推入队列中。例如: ```php $queue->push('job', 'Hello, Yaf Queue!'); ``` 4. 使用队列的 `pop` 方法从队列中获取任务。例如: ```php $job = $queue->pop('job'); if ($job) { echo $job->getBody(); // 输出:Hello, Yaf Queue! } ``` 5. 使用队列的 `ack` 方法确认任务已经被处理或者使用队列的 `nack` 方法拒绝任务。例如: ```php $queue->ack($job); // 或者 $queue->nack($job); ``` 通过以上步骤,可以简单地使用Yaf队列实现任务的入队、出队和处理等操作。可以根据具体需求,配置和扩展队列的驱动和选项,以满足不同的业务需求。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值