php
  • rest
  • yii2
  • 2017-06-27 4 views 2 likes 
    2

    yii\rest\UrlRuleというディレクトリをスキャンし、 "* Controller.php"という名前のファイルを許可されたREST APIコントローラのリストに追加するカスタム私たちのアプリケーション。クラスがカスタムYii2 UrlRuleのActiveControllerのサブクラスであるかどうかを確認するには

    use yii\rest\UrlRule; 
    class RestWildcardUrlRule extends UrlRule 
    { 
        public $path = '@app/services/controllers'; 
    
        public function init() 
        { 
         $d = dir(\Yii::getAlias($this->path)); 
         $controllers = []; 
         while (($entry = $d->read()) !== false) { 
          if(strpos($entry, 'Controller.php')) { 
           $controllers[] = strtolower(str_replace(['Controller.php'], '', $entry)); 
          } 
         } 
         $this->controller = $controllers; 
         parent::init(); 
        } 
    } 
    

    これにより、すべてのコントローラクラスに対して特定のUrlRulesを設定に書き込む必要がなくなります。今、私たちは、各コントローラは、特定の基本クラスを拡張するかどうかを確認する必要がある以外のすべては、うまく動作します:

    class SomeRestController extends RestControllerBase 
    

    は、私は私のUrlRule->init()方法では、各クラスのインスタンスを作成し、is_subclass_ofのようなものを使用する必要がありますか、またはそこにあります各コントローラが拡張するかどうかを確認する簡単な方法RestControllerBase

    答えて

    3

    あなたは

    get_parent_class (... ) 
    

    を使用することができ、オブジェクトの親クラスの名前を取得します。

    http://php.net/manual/en/function.get-parent-class.php

    それが唯一の直接の親をチェックアレックスBlex http://php.net/manual/en/function.class-parents.php

    +0

    BYT示唆した通り)(もclass_parent参照してください。 'class_parents'がより適切かもしれません –

    +0

    @AlexBlex .. correct ..あなたの提案でも更新されました。多くの感謝.. – scaisEdge

    +0

    ' get_parent_class'が私が探していた親クラスを返しましたが、私は 'class_parents' 。ありがとう! –

    関連する問題