2012-01-29 2 views
0

나는 여러 장소에서해야 할이 일이 : 가장 좋은 방법이 할 무슨젠드 프레임 워크 반복하지하기 위해이 작업을 수행하는 방법에 자신

$fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session 
    if(!$fbLogin->user) $this->_redirect('/'); #Logout the user 

:

public function init() 
{ 
    $fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session 
    if(!$fbLogin->user) $this->_redirect('/'); #Logout the user 
} 

이 두 줄을 ZendFramework에 있습니까? 플러그인을 만들려면 또는? 여러 곳에서 실행하고 싶지만 편집해야하는 경우 한 곳에서 편집하고 싶습니다.

답변

6

다음은 컨트롤러에서 쉽게 호출 할 수있는 Action Helper의 예입니다.

<?php 

class My_Helper_CheckFbLogin extends Zend_Controller_Action_Helper_Abstract 
{ 
    public function direct(array $params = array()) 
    { 
     // you could pass in $params as an array and use any of its values if needed 

     $request = $this->getRequest(); 
     $view = $this->getActionController()->view; 

     $fbLogin = new Zend_Session_Namespace('fbLogin'); #Get Facebook Session 
     if(!$fbLogin->user) { 
      $this->getActionController() 
       ->getHelper('redirector') 
       ->gotoUrl('/'); #Logout the user 
     } 

     return true; 
    } 
} 

사용하려면 헬퍼 중개인에게 어디에서 살 것인지 알려줘야합니다.

다음
// Make sure the path to My_ is in your path, i.e. in the library folder 
Zend_Loader_Autoloader::getInstance()->registerNamespace('My_'); 
Zend_Controller_Action_HelperBroker::addPrefix('My_Helper'); 

가 컨트롤러에서 사용하기 :

public function preDispatch() 
{ 
    $this->_helper->CheckFbLogin(); // redirects if not logged in 
} 

그것은 많은 세부로 이동하지 않지만, Writing Your Own Helpers가 도움이됩니다 여기에 당신이 그렇게 할 수있는 부트 스트랩에 넣어 수있는 예제 코드는 게다가. 당신이 기본 일 대신 확장되는 당신도 baseController을 설정할 수있는 모든 컨트롤러에서이 검사를해야하는 경우

+0

폴더 구조 란 무엇입니까? 컨트롤러/액션/도우미/My_Helper.php? – Uffo

+0

그 예제에서 가져온 프로젝트의 구조는'library/My/Helper/CheckFbLogin.php' – drew010

+0

입니다. 시도해 보겠습니다. ZF 폴더 구조를 이해하지 못했습니다 :) – Uffo

0

:

class My_Base_Controller extends Zend_Controller_Action 
{ 
    public function init() 
    { ... 

class IndexController extends My_Base_Controller 
{ ... 

은 기본 컨트롤러로 init() 시프트 및 모든 특정 컨트롤러에서 반복하지 않아도됩니다.

특정 컨트롤러에서 다양한 init()이 필요합니까?

class FooController extends My_Base_Controller 
{ 
    public function init() 
    { 
     parent::init(); 
     ...