Reputation: 3
I'm new to codeigniter. Currently i'm trying to create a blog. I used the .htaccess file to remove index.php from the url. There is no problem with that however.
My url routings are working fine for the posts controller. But it's not working with the new admin controller.
Here is the routings file:
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// My routes
$route['category'] = 'posts/category';
$route['(:any)'] = 'posts/index/$1';
$route['(:any)/(:any)'] = 'posts/view/$1/$2';
$route['admin'] = 'admin';
Here is the Admin controller:
class Admin extends CI_Controller {
public function index()
{
$this->load->view('admin/index');
}
}
Here is the index file:
<?php echo "hello"; ?>
I'm getting a 404 error when i'm trying to access http://localhost/admin
. But all other routings are working fine without any error.
Action
I've tried changing the default_controller to admin controller, then it's working fine. I'm getting the desired output on http://localhost
.
$route['default_controller'] = 'admin';
So what i'm missing here?
Upvotes: 0
Views: 45
Reputation: 2996
You are facing the problem because of the order or routing. In CodeIgniter order or the given routes matter. Let's look at your routing,
After category
route you have written this route,
$route['(:any)'] = 'posts/index/$1';
This route simply means that catch any route which is other than the default route or the category
route, So no matter what URL you want to go to it will always redirect you to above route.
To solve this issue change the order of your routing like this,
$route['admin'] = 'admin';
$route['category'] = 'posts/category';
$route['(:any)'] = 'posts/index/$1';
$route['(:any)/(:any)'] = 'posts/view/$1/$2';
Upvotes: 1