1
我想有一个全局功能,它可以在所有控制器以及树枝扩展中使用。 功能目前看起来是这样的:如何使用Symfony访问树枝扩展中的全局服务功能?
namespace Symfony\Bundle\FrameworkBundle\Controller;
class BaseController extends Controller {
/**
* Check if the user is assigned to one of the given airlines
*
* @param Array $airlines
*
* @return boolean
*/
public function HAS_AIRLINE(Array $airlines) {
if ($airlines == null)
return false;
// Get current user
$user = $this->get('security.context')->getToken()->getUser();
foreach ($airlines as $airline) {
if($user->hasAirline($airline))
return true;
}
return false;
}
我实现了这个功能到BaseController.php,这样我就可以在所有其他控制器使用。迄今为止这工作得很好。 现在我想在树枝的扩展中使用这个函数,但我不知道如何。我有以下功能在我的树枝延伸:
/*
* Check if the current user has one of the given roles by using the function provided by the base controller
*/
public function hasAirline($airlines= Array())
{
if($airlines == null)
return false;
// How can I now use the function from my BaseController?
return HAS_AIRLINE($airlines);
}
我已经尝试定义BaseController作为一种服务,并在树枝推广使用这项服务,但我不能管理。
有人能帮我吗?我觉得我还没有理解Symfony的一些东西,但是这些文档并没有真正帮助我。
谢谢 MRE
谢谢伙计! – MRE 2015-04-01 12:24:36