Answer the question
In order to leave comments, you need to log in
Symfony 3.3 don't understand how to properly call a service function?
Sorry, I'm terribly stupid.
There is a service:
namespace AppBundle\Services;
use Doctrine\DBAL\Connection;
class Permission extends BaseService
{
/**
* @var Connection
*/
private $connection;
/**
* Permission constructor.
* @param mixed ...$args
*/
public function __construct(...$args)
{
parent::__construct(...$args);
$this->connection = $this->em->getConnection();
}
/**
* @param string $role_name
* @return array
*/
public function getPermissionsByRoleName(string $role_name): array
{
$q = $this->connection->prepare('SELECT * FROM `permissions` WHERE `roleName` = ":roleName";');
$q->bindValue('roleName', $role_name);
$q->execute();
return $q->fetchAll();
}
}
namespace AppBundle\Services;
use Doctrine\Bundle\DoctrineBundle\Registry;
class BaseService
{
/**
* @var Registry
*/
protected $em;
/**
* @var Configure
*/
protected $config;
/**
* @var \AppKernel
*/
protected $kernel;
/**
* Configure constructor.
* @param Registry $em
* @param Configure $configure
* @param \AppKernel $kernel
*/
public function __construct(Registry $em, Configure $configure, \AppKernel $kernel)
{
$this->em = $em;
$this->config = $configure;
$this->kernel = $kernel;
}
}
$i = new Permission(new Registry, new Configure, new \AppKernel);
$i->getPermissionsByRoleName('....');
Answer the question
In order to leave comments, you need to log in
I'm working on 4.4, in my case I would call in the controller like this:
/**
* @Route("/", name="app_home", methods={"GET"})
* @param Permission $permission
*/
public function index(Permission $permission)
{
$permission->getPermissionsByRoleName('role_name');
}
private $permisson;
public function __construct(Permission $permission)
{
$this->permisson = $permission;
}
public function myFunc()
{
$this->permisson->getPermissionsByRoleName('role_name');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question