2016-12-24 13 views
1

登録後(ランダムに生成された認証コードによって)検証プロセスを実行しようとしていますが、コードを検証した後でも、登録時にデータベースに格納されているコードを使用します。例えば:ルートパラメータは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); 
} 

ご協力いただければ幸いです。

ありがとうございます!あなたのルートがそう

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

を定義している

+0

だから、それは一度だけ動作しますか?それとも特定の文字列ですか?エラーフローは特に明確ではない。 – yivi

+0

これは一度しか動作しません。 – user2101411

+0

エラーを表示するのに役立つ別のコードを貼ります – user2101411

答えて

1

、それは文字(大文字または小文字)で始める必要があり、任意の文字(さえなし)番号(文字、数字、アンダースコアが続き、そしてダッシュ)。

ので、有効なルート:

検証/ c42557235936ed755d3305e2f7305aa3/ABCDE

ベリファイ(あなたがしようとし1)

/N123-123

検証/ Z

検証/ X-1

など

これらのいずれかが動作するはずです。しかし、あなたは、あなたの質問に提供する他のコードは:

/と/ 3bc056ff48fec352702652cfa4850ac4

開始を確認し、それは文句を言わないあなたのルータによってキャッチさ。ルートと一致するようにコードを生成する方法を変更するか、コードと一致するようにルートを変更する必要があります。例えば:

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

これは経路の問題を処理しましたが、ページが表示されないエラーを表示しました(確認/ 3bc056ff48fec352702652cfa4850ac4) – user2101411

+0

私は分かりません。それがルートの世話をするなら、それはどのように404を与えることができますか?あなたの質問を編集して詳細を教えてください。 – yivi

+0

私は{32}を取り出して*を追加しました。 – user2101411

関連する問題