2016-12-24 43 views
1

我試圖在註冊後(通過隨機生成的驗證碼)進行驗證過程,但在驗證一個代碼後,即使我是驗證另一個代碼使用註冊時存儲在數據庫中的代碼。例如:路由參數僅適用於一個代碼Zend Framework 2

驗證/ c42557235936ed755d3305e2f7305aa3

工作正常,但當我嘗試使用其他代碼(如/檢驗/ 3bc056ff48fec352702652cfa4850ac4),它爲應用程序生成的默認佈局和什麼也不做。我不知道是什麼原因造成的。

這是我的代碼,我爲此。

VerifyController -

namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 


class VerifyController extends AbstractActionController 
{ 
    public $verify; 


    public function indexAction() 
    { 
     $code = $this->params()->fromRoute('code'); 

     if ($this->getVerifyInstance()->authenticateCode($code) !== false) { 
      $this->flashMessenger()->addSuccessMessage("Verification Successful, you can now login."); 

      return $this->redirect()->toRoute('verify', array('action' => 'success')); 
     } else { 
      $this->flashMessenger()->addErrorMessage("Oops! Something went wrong while attempting to verify your account, please try again."); 

      return $this->redirect()->toRoute('verify', array('action' => 'failure')); 
     } 
    } 


    public function successAction() 
    { 

    } 

    public function failureAction() 
    { 

    } 


    public function getVerifyInstance() 
    { 
     if (!$this->verify) { 
      $sm = $this->getServiceLocator(); 
      $this->verify = $sm->get('Application\Model\VerifyModel'); 
     } 

     return $this->verify; 
    } 
} 

VerifyModel -

namespace Application\Model; 


use Zend\Db\TableGateway\TableGateway; 
use Zend\Db\Sql\Sql; 
use Zend\Db\Sql\Insert; 
use Zend\Db\Adapter\Adapter; 


class VerifyModel 
{ 
    /** 
    * @var TableGateway 
    */ 
    protected $table_gateway; 


    /** 
    * @var mixed 
    */ 
    protected $code; 


    /** 
    * Constructor method for VerifyModel class 
    * @param TableGateway $gateway 
    */ 
    public function __construct(TableGateway $gateway) 
    { 
     // check if $gateway was passed an instance of TableGateway 
     // if so, assign $this->table_gateway the value of $gateway 
     // if not, make it null 
     $gateway instanceof TableGateway ? $this->table_gateway = $gateway : $this->table_gateway = null; 
    } 


    public function authenticateCode($code) 
    { 

     // authenticate the verification code in the url against the one in the pending_users table 
     $this->code = !empty($code) ? $code : null; 

     $select = $this->table_gateway->select(array('pending_code' => $this->code)); 

     $row = $select->current(); 

     if (!$row) { 
      throw new \RuntimeException(sprintf('Invalid registration code %s', $this->code)); 
     } else { 
      // verification code was found 
      // proceed to remove the user from the pending_users table 
      // and insert into the members table 
      $data = array(
       'username' => $row['username'], 
       'password' => $row['password'], 
      ); 

      $sql = new Sql($this->table_gateway->getAdapter()); 

      $adapter = $this->table_gateway->getAdapter(); 

      $insert = new Insert('members'); 

      $insert->columns(array(
       'username', 
       'password' 
      ))->values(array(
       'username' => $data['username'], 
       'password' => $data['password'], 
      )); 

      $execute = $adapter->query(
       $sql->buildSqlString($insert), 
       Adapter::QUERY_MODE_EXECUTE 
      ); 


      if (count($execute) > 0) { 
       // remove the entry now 
       $delete = $this->table_gateway->delete(array('pending_code' => $this->code)); 

       if ($delete > 0) { 
        return true; 
       } 
      } 
     } 
    } 
} 

路線:

'verify' => array(
    'type' => 'Segment', 
    'options' => array(
     'route' => 'verify/:code', 
     'constraints' => array(
      'code' => '[a-zA-Z][a-zA-Z0-9_-]*', 
     ), 

     'defaults' => array(
      'controller' => 'Application\Controller\Verify', 
      'action'  => 'index', 
     ), 
    ), 
), 

和佈局配置者在Module.php:

public function init(ModuleManager $manager) 
{ 
    $events = $manager->getEventManager(); 

    $shared_events = $events->getSharedManager(); 

    $shared_events->attach(__NAMESPACE__, 'dispatch', function ($e) { 
     $controller = $e->getTarget(); 

     if (get_class($controller) == 'Application\Controller\SetupController') { 
      $controller->layout('layout/setup'); 
     } else if (get_class($controller) == 'Application\Controller\MemberLoginController' || get_class($controller) == 'Application\Controller\AdminLoginController') { 
      $controller->layout('layout/login'); 
     } else if (get_class($controller) == 'Application\Controller\RegisterController') { 
      $controller->layout('layout/register'); 
     } else if (get_class($controller) == 'Application\Controller\VerifyController') { 
      $controller->layout('layout/verify'); 
     } 
    }, 100); 
} 

任何幫助,將不勝感激。

謝謝!

+0

那麼,它只能工作一次?或者爲那個特定的字符串?錯誤流程不是特別清楚。 – yivi

+0

它只能工作一次。 – user2101411

+0

我把另一個代碼,以幫助顯示錯誤 – user2101411

回答

1

您的路線定義

'options' => array(
     'route' => 'verify/:code', 
     'constraints' => array(
      'code' => '[a-zA-Z][a-zA-Z0-9_-]*', 
     ), 

所以,它應該以一個字母(大寫或小寫)開始,並且後面有任何(甚至沒有)字符(字母,數字,下劃線的數量和破折號)。

因此,有效的途徑:

驗證/ c42557235936ed755d3305e2f7305aa3(你在哪裏嘗試之一)

驗證/ ABCDE

驗證/ N123-123

驗證/ Z

verify/X-1

任何這些應該工作。但是,你在你的問題提供了其他代碼:

/驗證/ 3bc056ff48fec352702652cfa4850ac4

開始於,所以它不會被你的路由器被抓住。您需要更改生成代碼的方式,以便它們符合您的路線,或者更改路線以便與您的代碼相符。例如:

'options' => array(
     'route' => 'verify/:code', 
     'constraints' => array(
     'code' => '[a-zA-Z0-9][a-zA-Z0-9_-]{28,32}', 
), 
+0

確實照顧了路線問題,但現在它顯示的頁面未找到錯誤(verify/3bc056ff48fec352702652cfa4850ac4) – user2101411

+0

我不明白。如果它照顧了路線,它怎麼能給404一個?你能通過編輯你的問題來進一步闡述嗎? – yivi

+0

我拿出了{32}並添加了*,它工作。 – user2101411