路由参数仅适用于一种代码 Zend Framework 2

Route parameter only works for one code Zend Framework 2

我想在注册后进行验证(通过随机生成的验证码),但在我验证了一个代码后,即使我使用存储在注册后的数据库。例如:

verify/c42557235936ed755d3305e2f7305aa3 

...工作正常,但是当我尝试使用其他代码(如 /verify/3bc056ff48fec352702652cfa4850ac4)时,它会为应用程序生成默认布局并且什么都不做。我不知道是什么原因造成的。

这是我的代码:

验证控制器-

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;
    }
}

验证模型-

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_-]*',
       ),

因此,它应该以一个字母(大写或小写)开头,然后是任意(甚至 none)个字符(字母、数字、下划线和破折号)。

因此,有效路线:

verify/c42557235936ed755d3305e2f7305aa3 (the one you where trying)

verify/abcde

verify/N123-123

verify/Z

verify/X-1

等等

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

/verify/3bc056ff48fec352702652cfa4850ac4

数字 开头,因此它不会被您的路由器捕获。您需要更改生成代码的方式以使其与您的路线匹配,或者更改您的路线以使其与您的代码匹配。例如:

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