English 中文(简体)
编码协调动态路线
原标题:code igniter dynamic routing

我使用密码调子。

我想做的是,

如果访问网页不存在

例如:com/idontexist

然后,我想首先核对一个数据库,看idontexist是否在数据库中。

如果是这样,我就想把用户带往用户。

例如:com/action/idontexist。

我如何能够这样做?

最佳回答

One solution would be to extend the CI_Router class with your own in application/core/MY_Router.php and just copy the method _set_routing() in your class. Your changes would go somewhere after routes.php file is included and set to $this->routes property, you can add your custom 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 ];
}

Of course you might not need this, but I hope it gives you some idea. This way you can add custom routes and since you will have to fetch them from database, consider caching them for better speed.

问题回答

我感到,每星期都会问这个问题。

Open up your application/config/routes.php, then add something like this:

$route[ ^(:any) ] = "my_controller/get_article/$1";

Please note that it will route everything to a controller called action. If you have other controllers then you should add a route for them too (preferably placed before this one).

// EDIT: Using this you can goto http://your-site.com/secrets-of-internet-marketing and it will call the get_article function in the my_controller controller, and pass "secrets-of-internet-marketing" as the first argument. Which can then process with something like this:

public function get_article($article_name) { 
    // something like this: 
    $article = $this->article_model->get_model_by_name($article_name);
    $this->load->view( article , $article);
}




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签