2013-06-26 82 views
1

如何獲取參數值我需要在我下面的樹枝延伸類來獲得一個參數值我在樹枝擴展類的Symfony2

namespace xxxx\WebBundle\Twig; 
use Symfony\Component\HttpKernel\KernelInterface; 

class MyExtension extends \Twig_Extension 

{ 
    public function getFilters() 
    { 
     return array(
      new \Twig_SimpleFilter('affliate', array($this, 'urlFilter')), 
     ); 
    } 

    public function urlFilter($url,$aff) 
    { 
     $separator = "?"; 
     if (strpos($url,"?")!=false) { 
      $separator = "&"; 
     } 
     $parse = parse_url($url); 
     //echo $parse['host']; 

     $app_url = $url.$separator.'tag='.$aff; 

     return $app_url; 
    } 

    public function getName() 
    { 
     return 'wishbot_extension'; 
    } 
} 

在控制器,我們可以用這樣$這個 - >容器 - >的getParameter( 'CONTACT_EMAIL'); 但我需要樹枝擴展類中的值。

+0

沒有我的回答可以幫助您解決你的問題還是不清楚?如果是的請評論,否則請接受答案:) – nifr

回答

0

注入的參數作爲參數到您的樹枝延伸的構造是這樣的:

config.yml

services: 
    twig.extension.your_extension: 
     class: Vendor\YourBundle\Twig\Extension\YourExtension 
     arguments: [ %parameter1%, %parameter2% ]   
     tags: 
      - { name: twig.extension } 

YourExtension.php

protected $parameter1; 
protected $parameter1; 

public function __construct($parameter1, $parameter2) 
{ 
    $this->parameter1 = $parameter1; 
    $this->parameter2 = $parameter2; 
} 

// .... 

public function urlFilter($url,$aff) 
{ 
    // access the parameters using $this->parameter1 
} 
+0

是的,它的作品,謝謝:) –