【问题标题】:How to escape html in Phalcon\Mvc\View如何在 Phalcon\Mvc\View 中转义 html
【发布时间】:2012-12-18 04:47:56
【问题描述】:

我发现 phalcon 中的视图变量可以被 Phalcon\Escaper 转义:http://docs.phalconphp.com/en/latest/api/Phalcon_Escaper.html

例如,在 Zend 中,有一种方法可以从视图中调用视图助手:

// view context
$this->escape($data); // calls View\Helper\Escape
$this->url($params); // calls url view helper
// etc

有没有办法在不每次都创建新对象的情况下获得这样的视图助手? 我目前的想法是制作一些 BaseView 类,从 Phalcon\Mvc\View 扩展并在那里定义一些常用的方法,这些方法将使用缓存的对象..但我不确定这是最好的方法:

class BaseView extends Phalcon\Mvc\View
{
    // cached helper objects
    $helpers = [];

    // view helper call
    public function url($params)
    { 
       if (!$this->helpers['url']) {
           $this->helpers['url'] = new Phalcon\Mvc\Url();
       }
       return $this->helpers['url']->get($params);
    }
}

【问题讨论】:

    标签: php view-helpers phalcon


    【解决方案1】:

    您可以在视图中使用 $this 访问服务容器 (DI) 中的服务:

    <?php 
        echo $this->escaper->escape('<h1>Hello</h1>'); //Access Phalcon\Escaper 
    ?>
    
    <?php 
        echo $this->url->get('posts/index'); //Access Phalcon\Mvc\Url
    ?>
    

    只需使用在服务容器中注册的名称即可。如果您使用的是 Phalcon\DI\FactoryDe​​fault,以下是默认注册服务的列表:http://docs.phalconphp.com/en/latest/reference/di.html#service-name-conventions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多