0

したがって、この質問は、私が作成した別のものから来ています。ここで見つけることができますが、コードを変更したため、次のようになります。

カーコントローラー

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'];
    }

タイトルでテストしていますが、まったく表示されないようです。特定のカテゴリのドロップダウンメニューを表示したいと思います。

car-live.local/cars/Volvo ---> 「ボルボカー ファインダーへようこそ」

car-live.local/cars/BMW ---> 「 BMWカー ファインダーへようこそ」

現在、URLを介してIDを見つけているため、URLをさらに分割する必要があるため、機能していないことはわかっていますが、これを行う方法がわかりません: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);
        }

    }
4

1 に答える 1

1

問題を正しく理解できたかどうかはわかりませんが、コードから見ると、URL の処理に問題があるようです。それでは、次の例を見てみましょう。

car-live.local/car/Volvo

に到達した後、 で爆発する必要indexActionがあります。/

これは、URL のすべてのコンポーネントを含む配列を返します。次に、この場合は最後のものを取得してメソッドVolvoに送信しますfind_id_by_name()

この問題に対する私の通常のアプローチは、次のようなものに従うことです。

  1. スラッシュで爆発します。
  2. 最初のものを取得します (この場合car、それと残りの要求をコンストラクターに送信することにより、cars クラスに「ルーティング」します。
  3. 次に、carクラス コンストラクターは URL の次の部分を取得Volvoし、それを適切なメソッドや必要なものに "ルーティング" しgetVolvo()ます...

必要なものにルーティングすることはできますが、URL を処理する責任を適切なクラスに委任するようにしてください。例:

/cars/whatEverCar=> クラスごとに処理する必要がありますcar

/bike/whatEverBike=> クラスごとに処理する必要がありますbike

お役に立てれば。


編集:

メソッドfind_id_by_name()には、次のような URL を含めることができます。

/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()です。

警告: このコードには小さなバグがある可能性があります。メモ帳で書かれたものであり、テストされていません。

于 2013-04-30T13:39:05.703 に答える