Archives du mot-clef filter

Twig filter example

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/../src/Views',
    'twig.options'          => array(
        'charset'           => 'utf-8',
        'strict_variables'  => true,
        'cache'             => __DIR__.'/cache'
    )
));
 
$app->before(function() use($app) {
    $app['twig']->addExtension(new \Twig\MyExtension($app));
});
<?php
namespace Twig;
 
class MyExtension extends \Twig_Extension {
 
    public function getName() {
        return 'my';
    }
 
    public function getFilters() {
        return array(
            'link' => new \Twig_Filter_Method($this, 'link_filter', array('is_safe' => array('html'))),
        );
    }
 
    public function link_filter($ressource, $type = null) {
        $img_regex = '#(gif|jpe?g|png)$#i';
        $url_regex = '#^(https?:\/\/)#i';
        if(!$type) {
            if(preg_match($img_regex, $ressource)) {
                $type = 'img';
            } else if(preg_match($url_regex, $ressource)) {
                $type = 'a';
            }
        }
        switch($type) {
        case 'img':
            return '<img src="'.$ressource.'"/>';
            break;
        case 'a':
            return '<a href="'.$ressource.'">'.$ressource.'</a>';
            break;
        default:
            return $ressource;
        }
    }
}
<p>
{{ ressource|link }}
</p>

Symfony filter SSL

apps/frontend/config/app.yml

  ssl_filter:
    enabled: true
    secure:
      profile:
       actions: ~
      feed:
       actions: ~
      bookmark:
       actions: ~
      alert:
       actions: ~
      sfGuardAuth:
        actions: [signin]
      sfGuardRegister:
        actions: ~
      sfGuardForgotPassword:
        action: ~

lib/filter/sfSslFilter.class.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
class sfSslFilter extends sfFilter {
  public function execute($filterChain) {
    if(sfConfig::get('app_ssl_filter_enabled', true) && $this->isFirstCall()) {
      $request = $this->getContext()->getRequest();
      if($request->isMethod('get') || $request->isMethod('head')) {
        $context = $this->getContext();
        $controller = $context->getController();
        $stackEntry = $controller->getActionStack()->getLastEntry();
        $module = $stackEntry->getModuleName();
        $action = $stackEntry->getActionName();
        $moduleSettings = sfConfig::get('app_ssl_filter_secure', false);
        if(isset($moduleSettings[$module])) {
          if(isset($moduleSettings[$module]['actions'])){
            if(!$request->isSecure() && is_array($moduleSettings[$module]['actions']) && in_array($action, $moduleSettings[$module]['actions'])) {
              return $this->redirectSecure($request);
            }
          } else if(!$request->isSecure()) {
            return $this->redirectSecure($request);
          }
        }
      }
    }
    $filterChain->execute();
  }
 
  protected function redirectSecure(sfWebRequest $request) {
    $url = str_replace( 'http', 'https', $request->getUri());
    return $this->getContext()->getController()->redirect($url, 0, 301);
  }
}

apps/frontend/config/filters.yml

ssl:
  class: sfSslFilter