您好我的捆绑包中创建了以下帮助程序:
埃杜/ AccountBundle /服务/ Helper.php
namespace Edu\AccountBundle\Service;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Form\Form;
use Edu\AccountBundle\CommonFunctions\CommonFunctions;
class Helper{
private $session;
private $router;
private $documentManager;
private $commonFunctions;
/*
* Class:Constructor
* @DESC:its a very first method which always called whenever this call going to be instantiate
* @param : @session,@route,@db
* @sunilrawat@indivar.com
* @03-06-2016
*/
public function __construct(Session $session, Router $router, DocumentManager $dm)
{
$this->session = $session;
$this->router = $router;
$this->dm = $dm;
$this->commonFunctions = new CommonFunctions();
}
/*
* Class:checkFinancialYearLockPeriod
* @DESC:check a particular date on which accounts Financial Year data will be Locked So that user can not make any modifications after that
* @param : @db
* @sunilrawat@indivar.com
* @03-06-2016
*/
public function checkFinancialYearLockPeriod($dm){
$currentYearIs = $this->commonFunctions()->checkFinancialYear($this->dm);
$exploadsCurrentYear = explode("-",$currentYearIs);
$currentFinancialYearTo = "31-03-".$exploadsCurrentYear[1];
return $effectiveDate = date('d-m-Y', strtotime("+63 days", strtotime($currentFinancialYearTo)));
}
}
//My services.xml code is as following:
<service id="edu.account.helper" class="Edu\AccountBundle\Service\Helper">
<argument type="service" id="session"/>
<argument type="service" id="router"/>
<argument type="service" id="doctrine_mongodb.odm.document_manager"/>
<tag name="twig.helper" />
</service>
//So now I want to call it on Twig directly. So I am calking it as follows:
{{ edu.account.helper.checkFinancialYearLockPeriod() }}
但它没有像抛出以下错误一样工作: &#34;变量&#34; edu&#34;不存在&#34;。
请帮助解决此问题,提前致谢:
答案 0 :(得分:0)
Helper in Symfony2 not able to call on Twig 是完全正确的。
没有名为&#34; twig.helper&#34;的标签。只有一个名为&#34; twig.extension&#34;。如果要使用它,则应扩展\ Twig_Extension。请参阅http://symfony.com/doc/current/cookbook/templating/twig_extension.html。
但在您的情况下,您需要执行以下操作:
#app/config/config.yml
twig:
globals:
edu_account_helper: "@edu.account.helper" #key is name in twig, value is reference to your service
这意味着在Twig-Scope中,它被称为&#34; edu_account_helper&#34;。蛇套管。
{{ edu_account_helper.checkFinancialYearLockPeriod() }}