2013-03-19 73 views
0

我使用cake php version 1.3.11,並使用2臺一個是article_category和第二個是articles。 在文章類別使用與文章類別ID文章細節存儲物品的類別名稱和文章表。自定義URL蛋糕PHP

我創建了一個文章控制器列表類別明智的文章列表。現在該網址顯示是這樣的 mysite/articles/index/<article_category_id> 取而代之的是類型上市,我想這樣的 mysite/<article_category_name>/index 我怎樣才能做到這一點顯示網址?

回答

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

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) { 
    
    }