CodeIgniter中的完全动态URL

时间:2013-02-01 14:58:07

标签: php codeigniter

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

http://example.com/[product-category]/[product-name]

问题是还有像/checkout/step-1这样的非产品页面,这些仍然必须起作用。 实现这一目标的最佳方法是什么?

2 个答案:

答案 0 :(得分:3)

您在路由配置文件(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

此方法的缺点是您必须在此文件中定义所有路由,甚至是直接映射到控制器/操作的路由。更好的方法可能是让您的产品路线以“产品”开头,以便它们看起来像这样:

  

http://example.com/products/ [产品类别] / [产品名称]

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

$routes['products/(.*)/(.*)'] = 'product_class/product_method/$1/$2';

这样做更好,因为它不会强制您为站点中的每个控制器/操作组合定义concreate路由。

http://ellislab.com/codeigniter/user-guide/general/routing.html

答案 1 :(得分:2)

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

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