Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing in CodeIgniter for (:any)

I'm trying to make my CodeIgniter application work similarly to WordPress.

I want to be able to make these kind of URLs:

http://www.example.com/my-post-example

http://www.example.com/new-headline-here

http://www.example.com/i-love-stackoverflow

My routing:

$route['(:any)'] = "core/index/$1";

Which will call my Core controller and pass the page name into the index function.

I then lookup in my database for the page name and display the page to the user. So far so good.

However, there will be times when I want to call another controller. For example:

http://www.example.com/admin/edit_page/3

http://www.example.com/admin/settings

Now I assume my route will just grab all these rules and send them into my Core controller. Is there a way to make an exception for certain pages? Or is it a good idea to do this check inside my Core controller.

For example,

if ($page not in DB) {
   // Call controller/method
}

This seems a little redundant since I just want CodeIgniter to handle this.

like image 427
Oliwol Avatar asked May 11 '17 07:05

Oliwol


1 Answers

The routing rule you using it is OK for your purpose.

If you use http://www.example.com/admin/edit_page/3 this link it will send you admin controller and edit_page method.It will not use routes any rule.

However you will get one problem if your link looks like this

http://www.example.com/my-post-example/test

It will try to go my-post-example controller and test method.

Again http://www.example.com/admin will use routes any rule, means it will redirect your to core controller instead of admin/index. In that case your url should be http://www.example.com/admin/index

Finally If you call your other link with controller/method name it will be OK using your any rule

like image 127
Shaiful Islam Avatar answered Oct 17 '22 08:10

Shaiful Islam