yaf/extras

此包已被放弃,不再维护。未建议替代包。

Yet Another Framework 的扩展包

v0.3.2 2014-06-19 02:06 UTC

This package is not auto-updated.

Last update: 2022-08-01 18:22:46 UTC


README

安装

通过 Composer 安装

> composer require "yaf/extras:*"

类: RESTfulRouter

RESTful 路由器,提供一种快速注册带有 HTTP 方法 适配(RESTful)的 RewriteRoute 的方法。

$router = new RESTfulRouter

创建一个路由器。

$router->on($method, $url, $controller, $action)
  • $method 字符串:HTTP 方法名称,支持通配符 * 匹配所有方法。
  • $url 字符串
  • $controller 字符串:控制器类名称
  • $action 字符串:控制器的方法名称

提示。您可以在单个 on() 中注册多个方法,用空格分隔方法,例如 $router->on('get post', 'user/:id', 'user', 'show')

示例
class Bootstrap extends \Yaf\Bootstrap_Abstract {
    // default YAF style route registration
    function _initRoute(\Yaf\Dispatcher $dispatcher) {
        $router = $dispatcher->getRouter();
        
        // default yaf rewrite route
        $router->addRoute('dog', new \Yaf\Route\Rewrite('dogs', array('controller' => 'dog', 'action' => 'index')));
        $router->addRoute('dog_show', new \Yaf\Route\Rewrite('dogs/:id', array('controller' => 'dog', 'action' => 'show')));
        $router->addRoute('dog_create', new \Yaf\Route\Rewrite('dogs/create', array('controller' => 'dog', 'action' => 'create')));
        $router->addRoute('dog_destroy', new \Yaf\Route\Rewrite('dogs/:id/delete', array('controller' => 'dog', 'action' => 'destroy')));
    }

    
    // RESTful style
    function _initRESTfulRoute() {
        $router = new \Yaf\Extras\RESTfulRouter;
        $router->on('post', 'cat', 'cat', 'create');
        $router->on('get', 'cat/:id', 'cat', 'show');
        $router->on('delete', 'cat/:id', 'cat', 'destroy');
        
        $router->on('put patch', 'cat/:id', 'cat', 'update');

        $router->on('*', 'pig/:id', 'pig', 'what');
    }
}
严格模式

new RESTfulRouter(true) 以打开严格模式。

在此模式下,uri 将会严格匹配,并引入了新的 <name> 风格语法。

例如,'/user/<name>' 将匹配 '/user/micheal',但不会匹配 '/user/micheal/age',也不会匹配 '/user/micheal/'

<name:filter> 支持的类型过滤器。

当前过滤器

  • int:纯数字

例如,'/user/<id:int>' 将匹配 '/user/123,但不会匹配 '/user/micheal'

因为它基于 YAF\Route\Regex,因此可以使用一些正则表达式。目前您不能使用 '()',这会导致错误。

例如,'/blog/?' 将匹配 '/blog''/blog/'

类: AdaptiveView

根据视图文件的扩展名名称以不同的方式(渲染器)进行渲染。如果找不到匹配的渲染器,则使用默认的 Yaf Simple View 作为后备。

$view = new AdaptiveView($path = '{yaf.application.directory}/views/')
  • $path 字符串:视图文件所在路径,默认为 '{yaf.application.directory}/views/'

创建一个视图。

$view->on($extname, $renderder)
  • $extname 字符串:视图文件的扩展名
  • $renderer($file, $data) 函数:当匹配到 extname 时调用,应该 返回 渲染结果。

使用 extname 注册一个渲染器。

$view->*

其他常见方法请参阅 Yaf/View/Interface

示例

Bootstrap.php

class Bootstrap extends \Yaf\Bootstrap_Abstract {
    function _initView(\Yaf\Dispatcher $dispatcher) {
        $view = new \Yaf\Extras\AdaptiveView;
        $path = $view->getScriptPath();
    
        // plain text TEST
        $view->on('txt', function ($file, $data) use ($path) {
            return file_get_contents( $path .$file ) .' THIS IS JUST A TEST';
        });

        // twig
        $view->on('twig', function ($file, $data) use ($path) {
            $loader = new Twig_Loader_Filesystem($path);
            $twig = new Twig_Environment($loader);
            return $twig->loadTemplate($file)->render($data);
        });

        $dispatcher->disableView(); // disable auto-render
        $dispatcher->setView($view);
    }
}

现在您可以在 YourController.php 中使用此视图

class TestController extends \Yaf\Controller_Abstract {
    public function testAction() {
        $view = $this->getView();
        $view->assign('content', 'Hello World'); 
        $view->display('text.txt');
        // $view->display('text.twig'); // render use twig
    }
}

类:ExtendedController

基于 \Yaf\Controller_Abstract 的通用控制器,具有许多有用的方法;

flash

闪存消息

$this->flash($msg, $type = 'info')

为下一个请求存储消息

$this->flashNow($msg, $type = 'info')

对于当前请求

$this->getFlash()

检索当前请求的消息

示例
class FooController extends ExtendedController {
    public function indexAction() {
        $this->flash('yep', 'success');
        $this->flashNow('nope', 'error');

        foreach ($this->getFlash() as $flash) {
            echo $flash['msg']; // 'nope' in current, 'yep' for next
            echo $flash['type'];
        }
    }
}