蛋糕php的自定义URL

时间:2013-03-19 06:28:42

标签: cakephp

我使用cake php version 1.3.11并使用2个表,其中一个是article_category,第二个是articles。 在文章类别中存储文章类别名称和文章表格,用于具有文章类别ID的文章详细信息。

我创建了一个文章控制器来列出类别明智的文章列表。现在,网址显示是这样的  mysite/articles/index/<article_category_id> 而不是那种类型列表,我想像这样显示网址  mysite/<article_category_name>/index 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

好的,那里有几件事。

约定 follow Cookbook for more and better understanding

  1. 您的article_category应该只是categories
  2. article_category类型的名称用于HABTM类型关系的连接表(多对多关系)。即使作为连接表,它也应该是articles_categories。您可以使用Inflectore::pluralize('singularname')创建复数个案。 follow Cookbook for more
  3. CakePHP有一个Router Class,用于处理连接自定义路由。它位于App/Config/route.php。您可以使用它来满足几乎任何URL要求。

    现在找到解决方案,

    • 修改您的categories(案例中的article_category)表,以包含名为slug的字段。我建议使用slug与名称相比。
    • 定义新路线 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]+'
         )
      );
      
    • 修改您的控制器操作以接受slug

      public function index( $id, $slug = null ) {
      
      }