Route参数仅适用于一个代码Zend Framework 2

时间:2016-12-24 00:15:40

标签: zend-framework

我正在尝试在注册后进行验证过程(通过随机生成的验证码),但在我验证一个代码之后,即使我使用存储在数据库中的代码,它也不会验证另一个代码注册。例如:

验证/ c42557235936ed755d3305e2f7305aa3

工作正常但是当我尝试使用其他代码(例如/ verify / 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);
}

任何帮助将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:1)

您的路线已定义

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

因此,它应以字母(大写或小写)开头,后跟任何(甚至没有)字符数(字母,数字,下划线和短划线)。

所以,有效路线:

  

验证/ c42557235936ed755d3305e2f7305aa3(您尝试的那个)

     

验证/ ABCDE

     

验证/ N123-123

     

验证/ Z

     

验证/ X-1

任何一个都应该有用。但是您在问题中提供的其他代码:

  

/验证/ 3bc056ff48fec352702652cfa4850ac4

数字开头,因此路由器不会抓住它。您需要更改生成代码的方式,使其与您的路线匹配,或更改路线以使其符合您的代码。 E.g:

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