2014-02-21 47 views
0

所以,我在Zend框架是新的,我安裝ZfcUser,我想,當用戶沒有登錄到沒有接入例如一些路線:/博客/條/添加,如何在用戶未登錄zfcuser時排除某些路線?

其實我使用<?php if($this->zfcUserIdentity()) :?>檢查,如果用戶登錄,但我怎麼可以將用戶重定向到我的登錄路由/user如果是試圖訪問/博客/條/添加, 所以PLZ如果有人有任何想法,我將非常感激:)

回答

0

簡單方法:

控制器中:

if (!$this->zfcUserAuthentication()->hasIdentity()) { 
    return $this->redirect()->toRoute('route_to_login',array('param' => $param)); 
} 

一些更多: (AUTH後會zfcuset您重定向到以前的控制器)在module.config.php

'controllers' => array(
    'invokables' => array(
     'zfcuser' => 'Application\Controller\UserController', 
    ), 
... 
), 

下副本文件的供應商\ ZF-公共\ ZFC-用戶

\ SRC \ ZfcUser \控制器\ UserController.php 到模塊的應用(相同的文件夾中module.config.php)

刪除所有函數除了authenticateAction add: 使用Zend \ Session \ Container; 和更改 return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());

$redirect_session = new Container('redirect'); 
     $redirect = $redirect_session->redirect; 
     if($redirect!='') 
     { 
      unset($_SESSION['redirect']); 
      return $this->redirect()->toRoute($redirect, array('lang' => $lang)); 
     } 
     return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute(),array('param' => $param)); 

,並在控制器最後加

use Zend\Session\Container; 
... 
if (!$this->zfcUserAuthentication()->hasIdentity()) { 
      $redirect_session = new Container('redirect'); 
      $redirect_session->redirect = 'route_to_this_controller_action'; 
      return $this->redirect()->toRoute('route_to_login',array('param' => $param)); 
     } 
4

我所選擇的答案不同意,因爲它不是真正的實際做這類事情的每個控制器的每一個動作內你想拒絕訪問。

有兩個大型模塊用於此任務。第一個是BjyAuthorize,另一個是ZfcRbac。請檢查出來。 ZfcRbac是我的最愛(因爲我爲它寫了文檔),但它需要PHP 5.4+

+0

Thnx @Sam我很欣賞你的答案,是的,我同意你的觀點,我不能在每一個動作中做同樣的事情, – Mohammadov