Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save winzou/1122123 to your computer and use it in GitHub Desktop.
Save winzou/1122123 to your computer and use it in GitHub Desktop.
Abstract Controller
<?php
namespace Asso\AbstractBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* AbstractController
* @author winzou
*/
abstract class AbstractController extends Controller
{
/**
* Get a user from the security context
*
* @throws AccessDeniedException if no user is authenticated
* @return User
*/
protected function getUser()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if( ! is_object($user) || ! $user instanceof UserInterface )
{
throw new AccessDeniedException('User does not have access to this page.');
}
return $user;
}
}
<?php
namespace Asso\BookBundle\Controller;
use Asso\AbstractBundle\Controller\AbstractController;
/**
* BookController
* @author winzou
*/
class BookController extends AbstractController
{
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment