2009-12-16 6 views
31

젠드 프레임 워크를 사용하고 있습니다. 다음 명령문을 사용하여 다른 작업으로 리디렉션하고 있습니다.Zend : 매개 변수가있는 작업으로 리디렉션

$this->_helper->redirector('some_action'); 

위의 문장은 완벽하게 작동하며 'some_action'이 호출됩니다. 이제 나는 some_action에 몇 가지 매개 변수를 전달하려고합니다.

some_action?uname=username&[email protected] 

및 호출 된 동작에서 매개 변수를 가져 오는 방법. 일반적으로 다음과 같이합니다 :

$userName = $_REQUEST['uname']; 
$usermail = $_REQUEST['umail']; 

어떻게 수행합니까? 예제 코드를 작성하십시오. 감사합니다

답변

27

(단지 Sarfraz의 도우미 방법에 통과)를 사용 Zend_Controller_Action::redirect() 방법

$this->redirect('/module/controller/action/username/robin/email/[email protected]'); 

참고 : _redirect() 방법은 젠드 프레임 워크로 사용되지 않습니다 1.7. 대신 redirect()을 사용하십시오.

그리고 호출 된 액션

:

$username = $this->_getParam('username'); 
$email = $this->_getParam('email'); 

_getParam()는 매개 변수가없는 경우 기본으로 변수로 설정되어 두 번째 선택적 인수 걸립니다. 당신이 리디렉터으로 시도 할 수

6

당신이 시도 할 수 있습니다 :

$this->_redirector->gotoUrl('/my-controller/my-action/param1/test/param2/test2'); 
50

: 내가 얘기를 깜빡 했네요

public function indexAction() 
{ 
    $auth = Zend_Auth::getInstance(); 
    if ($auth->hasIdentity()) { 
     // Identity exists; get it 
     $identity = $auth->getIdentity(); 
     $this->_redirect('/user/profile/' . $identity->user_id); 
    } else { 
     $this->_redirect('/user'); 
    } 
} 

사용자 ID에 대한

$params = array('user' => $user, 'mail' => $mail); 
$this->_helper->redirector($action, $controller, $module, $params); 
+2

$ params에 도착하면 어떻게 검색합니까? –

4

또한 $의 PARAMS 말을 추가 할 수 있습니다처럼, 물론 이것은 당신이 라우팅 설정에있는 가정입니다 $ param을 수락하십시오. 예를 들어 라우팅은 위의 예에서 리디렉션되고있는 페이지에 대해 다음과 같이 보일 것입니다 :

/application/configs/application.ini

resources.router.routes.user-profile.route = /user/profile/:id 
resources.router.routes.user-profile.defaults.module = default 
resources.router.routes.user-profile.defaults.controller = user 
resources.router.routes.user-profile.defaults.action = profile 
당신이 PARAM을 얻을 방법
0

당신이 어디에 있느냐에 따라 다르다.

여기서 원하는 것을 달성하기 위해 $ param 요청을 잡을 필요가 없다. FlashMessenger 도우미를 사용하여 스택에 메시지를 추가하기 만하면됩니다. 그런 다음 메시지를 표시 할 작업 내에서 메시지를 검색 한 다음 successAction에서와 마찬가지로 메시지를 뷰에 할당합니다. $ this-> view-> var = $ var;와 같은 컨트롤러에 $ var 또는 배열 데이터를 할당하여 전달할 수 있습니다. 그러면 뷰 내에 $ this-> var로 액세스됩니다.

로그인에 대해 묻는 순간부터 나는 보통 어떻게하는지 알려줄 것입니다. 그게 최선의 방법은 아닙니다.우리가 이렇게 성공 액션에서

public function indexAction() { 
    $form = new Zfcms_Form_Login; 
    $this->view->form = $form; 
    /*check for valid input 
     authenticate using adapter 
     persist user record to session 
     redirect to original request URL if present*/ 
    if ($this->getRequest()->isPost()) { 
     if ($form->isValid($this->getRequest()->getPost())) { 
      $values = $form->getValues(); 

      $authAdapter = $this->getAuthAdapter(); 

      # get the username and password from the form 
      $username = $values['username']; 
      $password = $values['password']; 

      # pass to the adapter the submitted username and password 
      $authAdapter->setIdentity($username) 
        ->setCredential($password); 

      $auth = Zend_Auth::getInstance(); 
      $result = $auth->authenticate($authAdapter); 

      if ($result->isValid()) { 

       # all info about this user from the login table 
       # ommit only the password, we don't need that 
       $userInfo = $authAdapter->getResultRowObject(null, 'password'); 

       # the default storage is a session with namespace Zend_Auth 
       $authStorage = $auth->getStorage(); 
       $authStorage->write($userInfo); 


       $session = new Zend_Session_Namespace('zfcms.auth'); 
       if (isset($session->requestURL)) { 
        $url = $session->requestURL; 
        unset($session->requestURL); 
        $this->_redirect($url); 
       } else { 
        $this->_helper->getHelper('FlashMessenger') 
          ->addMessage('You were successfully logged in as ' . $userInfo->username); 
        $this->_redirect('/login/success'); 
       } 
      } else { 
       $this->view->message = 'You could not be logged in. Please try again.'; 
      } 
     } 
    } 
} 

: 우리는 내가 아래에 무엇을 같이 할 수있는보기 스크립트에서

public function successAction() { 
    if ($this->_helper->getHelper('FlashMessenger')->getMessages()) { 
     $this->view->messages = $this->_helper 
         ->getHelper('FlashMessenger') 
         ->getMessages(); 
    } else { 
     $this->_redirect('/login/success'); 
    } 
} 

내 인 LoginController의 인덱스보기는 양식을 보유하고 있습니다.

$this->view->message = 'message goes here'; 

을 그리고 그들이보기에 설정되어 두 경우를 잡을 :

내가 할 이유는이 방법은 때로는이 경우에는 단순히 사용하는 컨트롤러에 단 하나의 메시지를 전달하는 것입니다
<?php 
    if(isset($this->message) || isset($this->messages)): 
?> 

<?php 
if(is_array($this->messages)) 
{ 
    echo implode($this->messages); 
} else { 
    echo $this->message; 
}?> 

<?php 
endif 
?> 
관련 문제