自定义URL蛋糕PHP
问题描述:
我使用cake php version 1.3.11
,并使用2台一个是article_category
和第二个是articles
。 在文章类别使用与文章类别ID文章细节存储物品的类别名称和文章表。自定义URL蛋糕PHP
我创建了一个文章控制器列表类别明智的文章列表。现在该网址显示是这样的 mysite/articles/index/<article_category_id>
取而代之的是类型上市,我想这样的 mysite/<article_category_name>/index
我怎样才能做到这一点显示网址?
答
好吧,两件事情在那里。
约定follow Cookbook for more and better understanding
- 你
article_category
本来应该只是categories
。 -
article_category
类型名称用于连接的HABTM关系型表(许多一对多的关系)。即使作为一个连接表,它应该是articles_categories
。您可以使用Inflectore::pluralize('singularname')
来创建多个案例。 follow Cookbook for more
CakePHP有Router Class,它处理连接定制路由。它位于App/Config/route.php
。您可以使用它来满足几乎所有的URL要求。
现在的解决方案,
- 修改您
categories
(你的情况article_category)表包含一个称为slug
场。与名称相比,我会推荐使用slu g。 -
定义一个新的路线follow CookBook for more
// In app/Config/routes.php Router::connect( '/articles/index/:id-:slug', // E.g. /articles/index/3-technology array('controller' => 'articles', 'action' => 'index'), array( // order matters since this will simply map ":id" to $articleId in your action 'pass' => array('id', 'slug'), 'id' => '[0-9]+' ) );
-
修改控制器动作来接受塞
public function index($id, $slug = null) { }