首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在CodeIgniter中获得包含有用信息的URL结构?

问如何在CodeIgniter中获得包含有用信息的URL结构?
EN

Stack Overflow用户
提问于 2013-01-16 01:36:17
回答 1查看 474关注 0票数 1

我有一个网站,它有一个网址结构,这是没有任何帮助的面包屑,有利于搜索引擎优化,或直观的用户。它有点像

asdf.com/directory/listing/{unique_id}/{unique-page-name}/

我真的想把这个改成

asdf.com/{state}/{city}/{unique_id}/{unique-page-name}/

或者非常相似的东西。这样,我就可以以

Home > State > City > Company

有没有人有什么想法可以把当前的结构转换成我上面描述的结构?无论我怎么看,似乎都需要对网站进行彻底的改造。如果能向用户展示像Home > Florida > Miami > Bob's Haircuts这样的东西,那就太棒了

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-16 02:08:43

你只需要对你的路线有创意:http://ellislab.com/codeigniter/user-guide/general/routing.html

您可以设置一个路由来捕获所有流量,并将其指向directory/listing,然后在您的listing方法中-您可以手动访问url段。例如:

代码语言:javascript
复制
// application/config/routes.php
$route[':any'] = "directory/listing";
    /**
     you might have to play with this a bit, 
     I'm not sure, but you might need to do something like:
       $route[':any'] = "directory/listing";
       $route[':any/:any'] = "directory/listing";
       $route[':any/:any/:any'] = "directory/listing";
       $route[':any/:any/:any/:any'] = "directory/listing";
    */

// application/controllers/directory.php

function listing()
{
    // docs: http://ellislab.com/codeigniter/user-guide/libraries/uri.html
    $state = $this->uri->segment(1);
    $city = $this->uri->segment(2);
    $unique_id = $this->uri->segment(3);
    $unique_page_name = $this->uri->segment(4);

    // then use these as needed

}

或者,您需要能够调用其他控制器和方法-

您可以将URL更改为指向一个控制器,然后执行以下清单内容-

因此,您的url将变为:

代码语言:javascript
复制
asdf.com/directory/{state}/{city}/{unique_id}/{unique-page-name}/

你的路线会变成:

代码语言:javascript
复制
 $route['directory/:any'] = "directory/listing";

然后,您需要更新listing方法中的uri段以匹配第2、3、4和5个段。

这样,您仍然可以调用另一个控制器,并且它不会被您的自定义路由捕获:

代码语言:javascript
复制
asdf.com/contact/  --> would still access the contact controller and index method

更新

你也可以更有创意地使用正则表达式来捕获第一个uri段中带有状态名称的任何directory,然后将这些url推送到directory/listing,然后所有其他控制器仍然可以工作,并且您不必在url中添加url控制器。像这样的东西可能会起作用:

代码语言:javascript
复制
// application/config/routes.php
$route['REGEX-OF-STATE-NAMES'] = "directory/listing";
$route['REGEX-OF-STATE-NAMES/:any'] = "directory/listing"; // if needed
$route['REGEX-OF-STATE-NAMES/:any/:any'] = "directory/listing"; // if needed
$route['REGEX-OF-STATE-NAMES/:any/:any/:any'] = "directory/listing"; // if needed


/**
REGEX-OF-STATE-NAMES -- here's one of state abbreviations:
    http://regexlib.com/REDetails.aspx?regexp_id=471
*/
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14343476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档