【发布时间】:2019-06-18 22:05:02
【问题描述】:
假设我的网站中有这些网址
http://www.example.com/locations
http://www.example.com/locations/sydney
http://www.example.com/locations/brisbane
http://www.example.com/locations/perth
http://www.example.com/services/
http://www.example.com/services/cars
http://www.example.com/services/truck
http://www.example.com/services/phone
http://www.example.com/blog/display/blog-title
http://www.example.com/blog/display/blog-title2
http://www.example.com/blog/display/blog-title3
http://www.example.com/blog/display/blog-title4
我想要的是像这样的网址
http://www.example.com/locations
http://www.example.com/brisbane
http://www.example.com/services/
http://www.example.com/blog-title
http://www.example.com/blog-title2
http://www.example.com/blog-title3
http://www.example.com/blog-title4
如何在codeigniter中实现这一点??
我试过下面的代码:
$route[''] = 'blog/index';
$route['(:num)'] = 'blog/index/$1';
$route['(:any)'] = 'blog/display/$1';
$route['home/(:any)'] = 'blog/display/$1';
但问题是只为博客工作不能为其他人使用相同的代码,这会导致其他链接被破坏
所以我尝试@ACD 回答它给出了 2 个错误 1. 未定义属性:CI_Router::$load 2. null 时调用成员函数 library()
所以我做了一些这样的修改
$req = $this->uri->segment(1);
require_once ( BASEPATH. 'database/DB.php');
$db =& DB();
if ($db->get_where('home_inner_tbl', array('page_url' => $req))) {
$route['(:any)'] = 'home/display/$1';
} elseif ($db->get_where('blog_tbl', array('page_url' => $req))) {
$route['(:any)'] = 'blog/blog_display/$1';
} elseif ($db->get_where('towing_services_tbl', array('page_url' => $req))) {
$route['(:any)'] = 'towing-services/display/$1';
} elseif ($db->get_where('special_services_tbl', array('page_url' => $req))) {
$route['(:any)'] = 'cash-for-cars/display/$1';
} elseif ($db->get_where('locations_tbl', array('page_url' => $req))) {
$route['(:any)'] = 'locations/display/$1';
}
使用此代码,它没有给出错误,但只有 if 语句的第一行有效,即 $route['(:any)'] = 'home/display/$1';其他链接(例如定位、拖车服务、博客、现金换车)无效。
if 语句有问题吗?因为看起来这个答案应该是正确的。
顺便说一句,我的控制器看起来像这样
class Home extends CI_Controller
{
public function index() {
// some code in here
}
public function display($page_url) {
// some code in here
}
}
外观相同的控制器会转到其他人(位置、博客、拖曳服务等)
如您所见,我正在从数据库中调用 $page_url。
任何人都可以帮助我吗?谢谢
【问题讨论】:
标签: .htaccess codeigniter routes slug