2014-03-28 48 views
0

使用Fuelphp,我希望能夠在StackOverflow中使用與此處類似的URL系統來訪問特定頁面。Fuelphp - 帶參數的路由索引

期望的行爲: -stackoverflow.com/questions - >頁面,很多問題 -stackoverflow.com/questions/1982 - >頁面的具體問題

這裏是我的控制器:

class Controllers_Images extends Controller_Template 
{ 
    public function action_index($id=null) 
    { 
     if($id) 
     { 
     //Use Model_Image to find specific image 
     //Create appropriate view 
     } 
     else 
     { 
     //Use Model_Image to find many images 
     //Create appropriate view 
     } 
    } 
} 

我可以通過mysite.com/images/訪問「通用」頁面 - 這是路由到action_index函數。我可以通過mysite.com/images/index/1訪問「特定」頁面。我也想在這種情況下跳過index,以便mysite.com/images/1有效。現在我得到了404.我該如何設置路由?

回答

0

了一下週圍更戳後,我想出了正常工作的解決方案:

class Controllers_Images extends Controller_Template 
{ 
    public function action_index() 
    { 
     //Use Model_Image to find many images 
     //Create appropriate view 
    } 
    public function get_id($id) 
    { 
     //Use Model_Image to find specific image 
     //Create appropriate view 
    } 
} 

routes.php

return array(
    'images/(:num)'=>'images/id/$1', 
    //other routing stuff... 
); 

採用這種設置,該URL「mysite.com/images/1「現在適當地顯示所選圖像,就好像使用」mysite.com/images/id/1「一樣。不是一個大問題,但它是一個更好的界面,這很重要!