Home > Software engineering >  How to hide the Post Controller name & method from URL using Codeigniter 3
How to hide the Post Controller name & method from URL using Codeigniter 3

Time:10-15

I am using Codeigniter 3 and trying to hide the Controller file name and method from the URL but it's not working. In the below example "CommonPages" is the Controller file name and "postdetails" is the method.

www.mydomain.com/CommonPages/postDetails/social-testing-post
www.mydomain.com/CommonPages/postDetails/fix-the-windows-errors

I want to hide CommonPages/postDetails from the URL. There are around 100 of posts so it would be great if I do not specify each and every page link in the route.php file. Alternatively, I have tried the below code in route.php but it didn't worked for me.

$route['(:any)'] = 'CommonPages/postDetails/$1';
$route['(:num)'] = 'CommonPages/postDetails/$1';
$route['([a-zA-Z0-9] )'] = "CommonPages/postDetails/$1";

Thanks all.

CodePudding user response:

you can directly update your routes

$route['CommonPages/postDetails/(:any)'] = 'anyClass/anyMethod';

on anyMethod you can get the uri segment for getting param

public function anyMethod ()
        {
        print_r($this->uri->segments);
        }

CodePudding user response:

$route['product/:num'] = 'catalog/product_lookup';

In a route, the array key contains the URI to be matched, while the array value contains the destination it should be re-routed to.

In the above example, if the literal word product is found in the first segment of the URL, and a number is found in the second segment, the catalog class and the product_lookup method are instead used.

You need config better the routes check this for more info:

https://codeigniter.com/userguide3/general/routing.html

  • Related