CodeIgniter is one of the most popular PHP frameworks mostly used to build PHP Web Applications and REST APIs. Here on this blog post I’m going to write about how the CodeIgniter’s default controller/method route can be avoided.
Many people are facing this issue while building APIs or web applications on CodeIgniter. When you wanted to use predefined & custom URL on your routes. It is generally happens that the same thing is going to be used by both way,
- Your defined route
- controller/method
Let’s take an example, when your write a route $route[‘post/(:num)/edit’] = ‘post/edit/$1’;
to edit a blog post from a URL like ‘https://yourdomain.com/post/5/edit’ and to handle this route as defined, you wanted to use controller ‘post’ and method ‘edit($id)’. Now the case is the same controller and method with all same functionality is also possible to call by using CodeIgniter’s default controller/method route like ‘https://yourdomain.com/index.php/post/edit/5’ or ‘https://yourdomain.com/post/edit/5’ (if you have set $config['index_page'] = '';
).
As solution you can write another route $route['(.+)'] = 'errors/page_missing';
at the end of routes.php file which will be for each and every route request except the defined routes above. Here I have considered that 404 page is defined for ‘errors’ controller and ‘page_missing’ method.
So your routes.php file will be looks something like this,
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
* All comments and your defined routes
*/
$route['(.+)'] = 'errors/page_missing';
Leave A Comment