2013-04-30 44 views
0

PHP5讓頁面所以這個問題來自另一個我創造了你可以找到here但我已經改變了我的代碼,所以現在看起來是這樣的:所有類別

車載控制器

public function indexAction(){ 

     $category = new Application_Model_CarMapper(); 
     $gcat = $this->getRequest()->getPathInfo(); 

     //get id 

     $id = $category->find_id_by_name($gcat); 

     $this->view->title = $category->get_sub_cat_select($id); 
    } 

這是我在映射器中創建的一個新函數:

public function find_id_by_name($name){ 

     $select = $this->getDbTable()->query("SELECT * FROM car_category WHERE category_name = '{$name}'"); 
     $result = $select->fetchAll(); 

     if(!$result) { 
      return; 
     } 
     return $result[0]['ID']; 
    } 

我正在測試它的標題,但它ju st似乎根本不顯示。我想它顯示的下拉爲特定類別的菜單,如

汽車live.local /汽車/沃爾沃--->「歡迎沃爾沃尋車」

汽車生活。本地/汽車/ BMW --->「歡迎寶馬尋車」

我知道這是行不通的,因爲我必須更加被拆下來的網址,因爲現在它被發現通過ID的網址,但我不確定如何做到這一點:s你可以在這個任何光芒將非常感激..謝謝。

[編輯]

新代碼:

public function indexAction(){ 

     $category = new Application_Model_CarMapper(); 
     $gcat = explode("/", $this->getRequest()->getPathInfo()); 

     if(isset($gcat['category_name'])) { 

     $id = $category->find_id_by_name($gcat); 

     $this->view->title = $category->get_sub_cat_select($id); 
     } 

    } 
+1

因爲您不使用'array [index]'' – 2013-04-30 10:43:55

+0

因此您不需要'{$ name}'所以我將如何適應當前的代碼?另外,它是'indexAction'我正在尋找改變,而不是查詢本身.. – ash 2013-04-30 10:53:52

回答

1

我不知道如果我理解正確的話您的問題,但是從代碼看起來像你有對付URL問題。所以,讓我們借這個例子:

car-live.local/car/Volvo

後你得到indexAction你應該explode/

這是返回一個包含URL的所有組件的數組,然後您將最後一個在這種情況下將爲Volvo,並將其發送給您的find_id_by_name()方法。

我通常的辦法處理這一問題的方法是遵循這樣的:

  1. 斜線爆炸;
  2. 獲取第一個(在這種情況下car和「路線」到一個汽車類,發送到構造函數和請求的其餘部分;
  3. car類的構造函數,然後取URL的下一部分Volvo和「路線」它像getVolvo()或任何你需要一個合適的方法...

您可以路由到你需要什麼,而只是試圖委派處理的URL,以適當的類的責任。例如:

/cars/whatEverCar =>應該由類car

/bike/whatEverBike =>被處理應由類處理bike

希望這有助於。


編輯:

find_id_by_name()方法可以有像一些網址:

/car/find_id_by_name/Volvo

上午我解釋這口井?您可以直接使用URL的一部分並直接調用方法...併發送其餘的URL。


編輯2:代碼示例...

下面的代碼是你的問題一個很好的解決方案:

<?php 
// Call this class as soon the site stars 
class Router { 

    public function __construct() { 
     // In some really cool way get your full URL, for example I'm gonna use a string... 
     $fullURL = "example.com/Car/findCarById/Volvo"; 

     array_shift($fullURL); // removes "example.com", obvious stuff... 

     $explodedUrl = explode("/", $fullURL); 

     $targetClass = $explodedUrl[0]; // Temporary store the target is "Car" 
     array_shift($fullURL); // Removes "Car", we don't need it anymore... 

     $target = new $targetClass($fullURL); // call the Car class responsible for handling all the "Car" related stuff! 
    } 
} 

class Car { 

    public function __construct($request) { 
     // $request is an array with "findCarById", "Volvo" 
     $targetMethod = $request[0]; // Temporary store the target is "findCarById" 
     array_shift($request); 
     $this->$targetMethod($request); 
    } 

    private function findCarById($parameter) { 
     // $parameter is an array with "Volvo".. 
     // Now to your query and other cool stuff with $parameter[0]; 
     print "I'm the method findCarById() of the class Car called by the main router to find the ID for {$parameter}..."; 
    } 
} 

?> 

如果你需要更多的功能添加到您的Car類,只需添加另一種方法,然後用他的名字通過URL調用它,就像我使用findCarById()一樣。

警告:此代碼可能有輕微的錯誤,它寫在記事本上,未經測試。

+0

謝謝.. @ TCB13:以及你正在做某種意義上,我瞭解有關爆炸URL的一點,但然後一切都只是得到在那之後會感到困惑。另外,由於有很多類別,我沒有看到爲每種類型的汽車製造單獨的課程和獲得功能。再加上這不是客戶希望它的結構,因爲他希望按照他的條件完成。編輯了現在可以在上述編輯中看到的代碼,所以如果您能夠顯示從哪裏出發,我會非常感激? :) – ash 2013-04-30 14:05:43

+0

@ash就像我在編輯中告訴過你的一樣,只需用'find_id_by_name()'方法創建一個'car'類,並按照我的說明通過URL進行路由。 MainRouter(gets/car /)構建「car」類併發送over/find_id_by_name/Volvo /,然後構造函數從URL調用方法find_id_by_name()並傳遞/ Volvo部分。 – TCB13 2013-04-30 14:07:35

+0

噢,技術術語讓我感動..也許一個例子可以幫助或一步一步? – ash 2013-04-30 14:17:53

相關問題