Php CodeIgniter中的完全动态URL

Php CodeIgniter中的完全动态URL,php,codeigniter,Php,Codeigniter,我正在建立一个基于CodeIgniter的在线商店。我希望URL看起来像这样 http://example.com/[product-category]/[product-name] 问题是还有一些非产品页面,比如/checkout/step-1,这些页面仍然需要工作。 实现这一点的最佳方法是什么?您在路由配置文件(application/config/Routes.php)中定义的路由将按照它们定义的顺序执行。如果您先输入更具体的路线,那么最后可以使用通用的“一网打尽”路线 $routes[

我正在建立一个基于CodeIgniter的在线商店。我希望URL看起来像这样

http://example.com/[product-category]/[product-name]
问题是还有一些非产品页面,比如
/checkout/step-1
,这些页面仍然需要工作。
实现这一点的最佳方法是什么?

您在路由配置文件(application/config/Routes.php)中定义的路由将按照它们定义的顺序执行。如果您先输入更具体的路线,那么最后可以使用通用的“一网打尽”路线

$routes['checkout/step_(\d+)'] = 'checkout/step_$1';
// calls the checkout class, step_x method

$routes['(.*)/(.*)'] = 'product_class/product_method/$1/$2';
// calls the product class, and product method, passing category and name as parameters
这种方法的缺点是,您必须在此文件中定义所有路由,甚至是直接映射到控制器/操作的路由。更好的方法可能是让您的产品路线从“产品”开始,这样它们看起来就像这样:

$routes['products/(.*)/(.*)'] = 'product_class/product_method/$1/$2';
http://example.com/products/[产品类别]/[产品名称]

使用此方法,您可以定义仅适用于以下产品的路由规则:

$routes['products/(.*)/(.*)'] = 'product_class/product_method/$1/$2';
这更好,因为它不会强制您为站点中的每个控制器/操作组合定义concreate路由


在application/config/routes.php中,例如

$route['checkout/(:any)'] = "checkout/test_controller_method/$1";