0

我正在将一个(相当草率的)zend 表达性网站转换为本地餐厅的 zend 框架 3 网站。当我在富有表现力的网站上设置路由时,我会根据如下所示的查询参数加载位置。

website.com/location?location=xxx

在我的新网站上,路由看起来像这样

website.com/locations/view/xxx

我需要设置一个将旧网址重定向到新网址的路线。到目前为止,我已经设置了一个查找 /location?location=[/:location] 的路由,希望它能识别这个“Segment”路由并加载适当的 LocationsController。现在它给了我一个找不到路线的错误。

我的代码如下所示。

模块.config.php

namespace Application;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'router' => [
        'routes' => [
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'locations-old' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/location?location=[/:location]',
                    'defaults' => [
                        'controller' => Controller\LocationController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'locations' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/locations[/:action[/:location]]',
                    'constraints' => [
                        'action' => '[a-zA-Z]*',
                        'location'     => '[a-zA-Z]*',
                    ],
                    'defaults' => [
                        'controller' => Controller\LocationController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'locations.html' => [
                'type'    => Literal::class,
                'options' => [
                    'route'    => '/locations.html',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'location'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ],
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'about' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/about',
                    'defaults' => [
                        'controller' => Controller\AboutController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'employ' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/employ',
                    'defaults' => [
                        'controller' => Controller\EmployController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'news' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/news',
                    'defaults' => [
                        'controller' => Controller\NewsController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
    ],
];

位置控制器.php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Application\Model\StoreTable;
use Zend\View\Model\ViewModel;

class LocationController extends AbstractActionController
{
    private $table;

    public function __construct(StoreTable $table)
    {
        $this->table = $table;
    }

    public function indexAction()
    {
        if($_GET['location'] && $this->table->doesExist($_GET['location'])) {
            $location = $_GET['location'];
            $this->redirect()->toRoute('locations', ['action' => 'view', 'location' => $location])->setStatusCode(302);
        } else {
            $this->redirect()->toUrl('/#locations')->setStatusCode(301);
        }
    }

    public function viewAction()
    {
        $location = (string) $this->params()->fromRoute('location');

        $store = $this->table->getStore($location);

        return new ViewModel([
            'store' => $store,
        ]);
    }
}

任何帮助将不胜感激,如果需要,我可以提供更多信息。谢谢!

4

1 回答 1

0

如下配置您的路线,这将根据您给定的“ website.com/location?location=xxx”处理基于查询的 url,

在下面的路线中“ :key”变量意味着?location=xxx

          'locations-old' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/location/:key',
                    'defaults' => [
                        'controller' => Controller\LocationController::class,
                        'action'     => 'index',
                    ],
                    'constraints' => [
                        'key' => '[a-z0-9]+',
                    ],
                ],
                'child_routes' => [
                    'query' => ['type' => 'query'],
                ],
            ]
于 2018-07-20T11:14:36.133 回答