如何使用 Codeigniter 3 从 URL 中隐藏 Post 控制器名称和方法

How to hide the Post Controller name & method from URL using Codeigniter 3

我正在使用 Codeigniter 3 并试图从 URL 中隐藏控制器文件名和方法,但它不起作用。在下面的示例中,“CommonPages”是控制器文件名,“postdetails”是方法。

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

我想在 URL 中隐藏 CommonPages/postDetails。大约有 100 个帖子,所以如果我不在 route.php 文件中指定每一页 link 就好了。或者,我在 route.php 中尝试了以下代码,但它对我不起作用。

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

谢谢大家

您可以直接更新您的路线

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

在任何方法上你都可以获得用于获取参数的 uri 段

public function anyMethod ()
        {
        print_r($this->uri->segments);
        }
$route['product/:num'] = 'catalog/product_lookup';

在路由中,数组键包含要匹配的 URI,而数组值包含它应该重新路由到的目的地。

在上面的示例中,如果在 URL 的第一段中找到字面词 product,并且在 中找到一个数字第二段,使用 catalog class 和 product_lookup 方法。

您需要更好地配置路由,检查此以获取更多信息:

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