I am using code igniter.
What I want to do is,
if a page is visited that does not exist
example.com/idontexist
Then I want to first check a database to see if idontexist
is in the database.
If it is then I want to route the user to
example.com/action/idontexist.
How can I do this?
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.
I feel like this gets asked every week.
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);
}