05/01/2017 - SYMFONY
Aşağıdaki örnek ile doğrulanmış veya login olan kullanıcının bilgilerine ulaşabilirsiniz.
Bu sadece bir örnek bu nedenle bir controller yerine, event listener de kullanabilirsiniz.
namespace Application\ServerBundle\Controller;
use Application\ServerBundle\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
/**
* @Route("api", service="application_server.controller.api")
*/
class ApiController extends Controller
{
private $tokenStorage;
public function __construct(
TokenStorageInterface $tokenStorage
) {
$this->tokenStorage = $tokenStorage;
}
/**
* @param Request $request
*
* @Method({"POST"})
* @Route("")
*
* @return Response
*/
public function indexAction(Request $request)
{
$user = $this->tokenStorage->getToken()->getUser();
if (!$user instanceof User) {
throw new AccessDeniedException();
}
$user->getId();
$user->getUsername();
$user->getEmail();
$user->getRoles();
$user->getPassword();
$user->getRoles();
// And setters ...
.....
return new Response(.....);
}
}
services:
application_server.controller.api:
class: Application\ServerBundle\Controller\ApiController
arguments:
- @security.token_storage