Php 使SEO敏感URL(避免id)Zend框架

Php 使SEO敏感URL(避免id)Zend框架,php,zend-framework,url,seo,Php,Zend Framework,Url,Seo,我有这样的url: http://quickstart.local/public/category1/product2 在url(category1/product2)中,编号是id,从数据库中获取的类别和产品注意id id是唯一的 我需要像zend framework url这样的敏感url。例如:http://stackoverflow.com/questions/621380/seo-url-structure 如何将该url转换为如下所示的新url 有办法吗 您需要在数据库中存储一个具

我有这样的url:

http://quickstart.local/public/category1/product2
在url(category1/product2)中,编号是id,从数据库中获取的类别和产品注意id

id是唯一的

我需要像zend framework url这样的敏感url。例如:
http://stackoverflow.com/questions/621380/seo-url-structure

如何将该url转换为如下所示的新url


有办法吗

您需要在数据库中存储一个具有字段名(如“url”或类似名称)的唯一值。每次生成新产品时,您都必须创建此唯一url并将其与产品信息一起存储。一种常见的方法是使用产品名称并使其url友好:

public function generateUrl($name)
{
    $alias = str_replace(' ', '-', strtolower(trim($name)));
    return preg_replace('/[^A-Za-z0-9-]/', '', $alias);
}
调用此方法:

$url = $this->generateUrl("My amazing product!");
echo $url;
将输出:

我的神奇产品

您需要检查此函数的输出是否在数据库中不存在,因为您将使用此值而不是id进行查询

如果您也将此逻辑应用于类别,那么您可以轻松地获得如下所示的可读性和描述性URL。不过,在正确工作之前,您可能需要调整路由

您可以使用ZF的。例如,要创建与SO使用的url类似的url,可以在application.ini中定义自定义路由,如下所示(假设控制器和操作分别名为questions和show):

有了这样的路由,您可以在视图中生成url,如下所示:

<?php echo $this->url(array('id'=>621380,'title' => 'seo url structure'),'questions');
// results in: /myapp/public/questions/621380/seo+url+structure

//OR if you really want to have dashes in your title:

<?php echo $this->url(array('id'=>621380,'title' => preg_replace('/\s+/','-','seo url structure'),'questions');
// results in: /myapp/public/questions/621380/seo-url-structure
然后将生成此路由的url:

<?php echo $this->url(array('category' => 6213,'product' => 80,'title' => preg_replace('/\s+/', '-', 'seo url structure')),'questions' );    ?>
// results in: /myapp/public/questions/6213-80/seo-url-structure

//搜索结果:/myapp/public/questions/6213-80/seo url结构

希望这将有助于或至少为您指明正确的方向。

仅是一个注释-产品url(用于eshop)不应包含类别,因为它可能会更改,然后它可能没有任何意义或更改url,从而破坏任何反向链接;)@神智清醒。一个原因可能是urlencode函数。url帮助器正在使用它对字符串进行编码。您可以通过将last url helper的参数设置为false来关闭它,例如
$this->url(数组('category'=>6213,'product'=>80,'title'=>preg_replace('/\s+/','-','seo url structure'),'questions',false,false)第三个参数的值默认为false,而第四个参数的值默认为true。也许这会有帮助。这个答案真的很好,很多。但我有一个问题,当标题显示字符转换为未知字符时,像这样%231607%3B%26%231608%3B%26%23157保存在db中的语言是带utf8的波斯语。当我使用justecho函数时,一切都正常,但当我使用$this->url函数时,字符有问题
resources.router.routes.questions.route = '/questions/(\d+)-(d+)/(\w*)'
resources.router.routes.questions.type = "Zend_Controller_Router_Route_Regex" 
resources.router.routes.questions.defaults.module = default
resources.router.routes.questions.defaults.controller = questions
resources.router.routes.questions.defaults.action = show
resources.router.routes.questions.map.1 = category
resources.router.routes.questions.map.2 = product
resources.router.routes.questions.map.3 = title
resources.router.routes.questions.reverse = "questions/%d-%d/%s"
<?php echo $this->url(array('category' => 6213,'product' => 80,'title' => preg_replace('/\s+/', '-', 'seo url structure')),'questions' );    ?>
// results in: /myapp/public/questions/6213-80/seo-url-structure