Restricting Access to All Pages Using Zend 2 and ZfcUser

A new application that I’m working on needs to block all access to every page, except the login page, for unauthenticated users. After setting up Zend 2 with the ZfcUser module, this is a piece of cake. Just update your module/Application/Module.php file with the following code:

$moduleRouteListener = new ModuleRouteListener();

$e->getApplication()->getEventManager()->getSharedManager()->attach('ZendMvcControllerAbstractActionController', 'dispatch', function($e) {

$controller = $e->getTarget();
$sm = $e->getApplication()->getServiceManager();
$auth = $sm->get('zfcuser_auth_service');

if (!$auth->hasIdentity()) {

// If on login / registration page, let them pass
!($e->getRouteMatch()->getParam('controller', 'index') == 'zfcuser' && $e->getRouteMatch()->getParam('action', 'index') == 'login')
&& !($e->getRouteMatch()->getParam('controller', 'index') == 'zfcuser' && $e->getRouteMatch()->getParam('action', 'index') == 'register'))
}, 100);


Wala. All requests, that are not to the login or register pages, are now forwarded to http://yoursite/user/login if a user is not authenticated.

Create a Title / Subtitle Pair in from a Controllers Name</a></h2> </header><!-- .entry-header --> <div class="entry-content"> <p>If I have a website called Blog with two main sub pages; about me and bio, I like to have the HTML title to always be My Site: Current page. Since all of my controllers have descriptive names, I can just put the following in my layout:</p> <p><code lang="php"><br /> <title>Blog: <?php echo ucwords(str_replace("-", " ", Zend_Controller_Front::getInstance()->getRequest()->getControllerName()));?>

This would give me the following when users navigate to my site:

Blog (
Blog: About Me (
Blog: Bio (