简体   繁体   English

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

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

I am using Codeigniter 3 and trying to hide the Controller file name and method from the URL but it's not working.我正在使用 Codeigniter 3 并试图从 URL 中隐藏控制器文件名和方法,但它不起作用。 In the below example "CommonPages" is the Controller file name and "postdetails" is the method.在下面的示例中,“CommonPages”是控制器文件名,“postdetails”是方法。

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.我想从 URL 中隐藏CommonPages/postDetails 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.大约有 100 个帖子,所以如果我不在route.php文件中指定每个页面链接会route.php Alternatively, I have tried the below code in route.php but it didn't worked for me.或者,我在route.php尝试了以下代码,但它对我不起作用。

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

Thanks all.谢谢大家。

$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.在路由中,数组键包含要匹配的URI ,而数组值包含它应该重新路由到的目的地。

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.在上面的例子中,如果在URL 的第一段中找到文字词product而在第二段中找到一个数字,则使用catalog类和product_lookup方法。

You need config better the routes check this for more info:您需要更好地配置路由以获取更多信息:

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

you can directly update your routes你可以直接更新你的路线

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

on anyMethod you can get the uri segment for getting param在 anyMethod 上,您可以获得用于获取参数的 uri 段

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM