0

Zend Framework アプリケーションをモノユーザーからマルチユーザーに切り替えています。

コントローラーにユーザースコープを含めるための最良のアプローチは何ですか?

1 つの方法は、すべてのコントローラーの各メソッドにユーザー ID を追加することです。

/application/controllers/IndexController.php
...
public function indexAction() {
    $params['user_id'] = Zend_Auth::getInstance()->getIdentity()->id;
    $listHelper->readItems($params);
}
...

もう 1 つは、新しい User モデルを作成し、彼のアイテムをフェッチすることです。

/application/controllers/IndexController.php
...
public function indexAction() {
    $userModel = new application_models_user();
    $userModel->find(Zend_Auth::getInstance()->getIdentity()->id);
    $userModel->readItems();
}
...

最小限のコードを記述できるようにするための最良のアプローチは何か、ユーザースコープ(dbスコープ、プラグインなど)を「自動的に」追加する別のアイデアがあるかどうか疑問に思っています。

4

1 に答える 1

1

Zend_Controller_Action を拡張して抽象クラスを作成する

abstract class My_Controller_Action extends Zend_Controller_Action  {


private $userModel;

public function getUserModel() {
 if(is_null($this->userModel)) $this->userModel  = new application_models_user();
 return $this->userModel;

}
public function getUserId() {
  return $this->getUserModel()->find(Zend_Auth::getInstance()->getIdentity()->id);
}

}

このクラスをコントローラーの基本クラスとして使用します。

于 2013-05-22T15:05:58.177 に答える