2011-04-21 74 views
0

我正在使用代码点火器。代码点火器动态路由

我想要做的是,

如果访问的网页不存在

example.com/idontexist

然后我想首先检查数据库,看看是否idontexist是在数据库中。

如果然后我要路由用户

example.com/action/idontexist.

我怎样才能做到这一点?

回答

2

一个解决办法是用自己的扩展CI_Router类应用/核心/ MY_Router.php和刚才复制的方法_set_routing()在你的类。 您的更改会在routes.php文件包含并设置为$ this-> routes属性后进入某处,您可以添加自定义路由。

 
include(APPPATH.'config/routes'.EXT); 
... 
$this->routes = (! isset($route) OR ! is_array($route)) ? array() : $route; 
unset($route); 

//Get your custom routes: 
$your_routes = $this->_get_custom_routes(); 
foreach($your_routes as $custom_route) 
{ 
    $this->routes[$custom_route['your_regex_match']] = $custom_route['controller'].'/'.$custom_route['action']; 
} 

当然,你可能不需要这个,但我希望它给你一些想法。 这样您可以添加自定义路由,因为您必须从数据库中获取它们,请考虑缓存它们以获得更好的速度。

+0

现在,当我尝试使用'get_instance()'从db加载URL来加载配置项时,出现此错误:致命错误:在'线233'上找不到系统\ core \ CodeIgniter.php中的类'CI_Controller' – 2013-11-22 12:04:42

3

我觉得每周都会问这个问题。

打开你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); 
} 
+0

不完全是我问的。 我不希望由静态文件驱动。 这个想法是,我可以添加一篇文章到数据库,然后成为一个路线,但我不想有/文章/ acticle /无论。明白我的观点? – Hailwood 2011-04-21 05:04:20

+0

您必须在控制器内部进行检查。这只是将请求路由到您的控制器,并让您访问输入。只需参数'$ 1',然后在数据库中查找它,然后返回该文章。 – icchanobot 2011-04-21 06:03:10