【发布时间】:2011-04-21 01:19:18
【问题描述】:
我正在使用代码点火器。
我想做的是,
如果访问的页面不存在
example.com/idontexist
那我想先检查一个数据库,看看idontexist是否在数据库中。
如果是,那么我想将用户路由到
example.com/action/idontexist.
我该怎么做?
【问题讨论】:
标签: mysql codeigniter routes
我正在使用代码点火器。
我想做的是,
如果访问的页面不存在
example.com/idontexist
那我想先检查一个数据库,看看idontexist是否在数据库中。
如果是,那么我想将用户路由到
example.com/action/idontexist.
我该怎么做?
【问题讨论】:
标签: mysql codeigniter routes
我觉得每周都会有人问这个问题。
打开你的application/config/routes.php,然后添加如下内容:
$route['^(:any)'] = "my_controller/get_article/$1";
请注意,它会将所有内容路由到名为action 的控制器。如果你有其他控制器,那么你也应该为它们添加一个路由(最好放在这个之前)。
// 编辑:使用它你可以转到http://your-site.com/secrets-of-internet-marketing,它将调用my_controller控制器中的get_article函数,并将"secrets-of-internet-marketing"作为第一个参数传递。然后可以像这样处理:
public function get_article($article_name) {
// something like this:
$article = $this->article_model->get_model_by_name($article_name);
$this->load->view('article', $article);
}
【讨论】:
$1,然后在您的数据库中查找它,然后返回该文章。
一种解决方案是在 application/core/MY_Router.php 中使用您自己的扩展 CI_Router 类,然后在您的类中复制方法 _set_routing()。 在包含 routes.php 文件并将其设置为 $this->routes 属性后,您的更改将在某处进行,您可以添加自定义路由。
包括(APPPATH.'config/routes'.EXT); ... $this->routes = ( !isset($route) OR !is_array($route)) ?数组():$路由; 未设置($路由); //获取您的自定义路线: $your_routes = $this->_get_custom_routes(); foreach($your_routes 作为 $custom_route) { $this->routes[$custom_route['your_regex_match']] = $custom_route['controller'].'/'.$custom_route['action']; }当然你可能不需要这个,但我希望它能给你一些想法。 通过这种方式,您可以添加自定义路由,并且由于您必须从数据库中获取它们,因此请考虑缓存它们以获得更快的速度。
【讨论】:
get_instance() 加载 CI 以从 db 加载 URL 时,我收到此错误:Fatal error: Class 'CI_Controller' not found in system\core\CodeIgniter.php on line 233