【问题标题】:Zend Framework URL Rewrite For SEOZend 框架 URL 重写为 SEO
【发布时间】:2016-03-02 20:48:40
【问题描述】:

我是 Zend 的新手,所以请帮我解决这个问题,因为我已经搜索了很多并尝试了很多,我真的做到了。

这是一个非常古老的问题:

这是我当前的网址:

www.sample.com/blog/detail/index/id/5

想要:

www.sample.com/url-rewrite-tips

好吧,我把下面的代码放在 bootstrap.php 中

$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route(
                'url-rewrite-tips',
                array(
                     'module'     => 'blog',
                     'controller' => 'detail',
                     'action'     => 'index',
                     'id'         => '5'
                )
);
$router->addRoute('url-rewrite-tips', $route);

它工作正常,但它也是一个硬代码。我试图在 bootstrap.php 中获取参数,但失败了。

现在,我有 100 多个 id。但是,我尝试将它放在 index.pthml 中的 foreach(){} 中,然后它失败了。 每篇文章我都在数据库中重写了名字,我该怎么做呢?

最好不要使用 configs 或 .htaccess

提前致谢。

【问题讨论】:

  • 您在引导程序中尝试获取的参数是如何以及失败的?在引导期间路由尚未发生,因此您无法从那里获取任何 URL 参数,因为它们尚未被查看。

标签: php zend-framework url-rewriting seo url-routing


【解决方案1】:

我总是在引导程序中添加 routes.ini 执行:

protected function _initRoutes()
{
    $this->bootstrap('FrontController');
    $front = $this->getResource('FrontController');
    $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'production');
    $router = $front->getRouter();
    $router->addConfig($config, 'routes');
}

然后在routes.ini:

[production]

routes.content.route = "/s/:permalink"
routes.content.defaults.controller = "content"
routes.content.defaults.action = "show"
routes.content.reqs.permalink = "[a-z0-9-]+"

所以现在当有人访问http://mydomain.com/s/some-nice-permalink 时,路由会转到ContentController ShowAction() 方法,该方法将永久链接作为参数并在数据库中找到它。

当然,您还必须编写在数据库中创建唯一永久链接的方法。

【讨论】:

    猜你喜欢
    • 2012-06-30
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多