Zend框架重定向列表网址

时间:2015-07-20 09:11:11

标签: php .htaccess redirect zend-framework

我想重定向我的Zend Framework 1应用程序中的url列表。现在我可以将所有数百个重定向添加到htaccess文件中,如下所示:

Redirect 301 /old-page.html /new-page.html

但我宁愿创建一个包含所有重定向的有组织的文件。这可能吗?我读了一些关于.ini文件的内容,但我想这并不是我想要的。

类似于一个数组,其中旧的url作为键,而新的url作为值也会很好。但是我对Zend框架很新,所以也许有人可以帮助我在这里?我猜我需要创建一个PHP文件并将其加载到引导程序中,但我对此非常挣扎。

编辑:

从我的头脑中,我想这样的事情会很好:

rewrites.php

$rewrites = array(
    '/old_url.html' => '/new_url.html'
);

if(array_key_exists(Zend_Controller_Front::getInstance()->getRequest()->getRequestUri(), $rewrites)){
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: ".$rewrites[Zend_Controller_Front::getInstance()->getRequest()->getRequestUri()]);
}

1 个答案:

答案 0 :(得分:2)

您可能想要做的是注册一个插件。然后,所述插件将检查传入的请求,如果满足某些条件,则重定向请求。

<强>库/应用/控制器/插件/ RedirectHandler.php

<?php

class App_Controller_Plugin_RedirectHandler
    extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopStartup
        (Zend_Controller_Request_Abstract $request)
    {
        // best to load this from somewhere, but we'll
        // put it here for illustration purposes
        $bindings = array(
            '/old_url.html' => '/new_url.html'
        );

        $uri = $request->getRequestUri();

        if (isset($bindings[$uri])) {
            $this->getResponse()
                ->setRedirect($bindings[$uri], 301)
                ->sendResponse();
            exit;
        }
    }
}

然后我们需要确保调用处理程序。

<强>应用/ bootstrap.php中

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    // ...

    protected function _initControllerPlugins ()
    {
        Zend_Controller_Front::getInstance()
            ->registerPlugin(new App_Controller_Plugin_RedirectHandler());
    }
}