title: model exist or not in YII using a custom function - Performance
body: <p>I am writing an REST API and need to check weather the given model name exist or not in the directory.</p>
  
  <p>1 - <a href="http://example.com/RestApi/index.php/api/posts/" rel="nofollow">http://example.com/RestApi/index.php/api/posts/</a></p>
  
  <p>2 - <a href="http://example.com/RestApi/index.php/api/post/" rel="nofollow">http://example.com/RestApi/index.php/api/post/</a></p>
  
  <p>from these two, URL 1 is incorrect and URL 2 is correct (post). so i am taking this param and doing a search as following,</p>
  
  <pre><code>  $model = $m::model()-&gt;findAll($criteria);
    $m = TK::get('model');
  </code></pre>
  
  <p>when $m is incorrect, PHP warning include(posts.php): failed to open stream: No such file or directory will trigger.</p>
  
  <p>so for that not to happen, i have written a function as modelExists() and using it like below.       </p>
  
  <pre><code>If (!TK::modelExists($m))
              $this-&gt;sendResponse(false, 1003);
  </code></pre>
  
  <p>the function body is as below,</p>
  
  <pre><code>/**
       * Checks for a given model name
       * @param string $modelName is the name of the model that is used to search against the directory.
       * @return String $result, the name of Model. if doesnt exist NULL;
       */
      public static function modelExists($modelName)
      {
          $result = null;
          $basePath = Yii::getPathOfAlias('application').DIRECTORY_SEPARATOR;
          $modelsDir = 'models'.DIRECTORY_SEPARATOR;
          $modelName = strtolower($modelName).'.php';
          $generalModelDir = scandir($basePath.$modelsDir);
  
          foreach ($generalModelDir as $entry) { // Searching in General model directory
              if ($modelName == strtolower($entry)) {
                  $temp = explode('.', $entry); // array('User','php')
                  $result = $temp[0];
                  break;
              }
          }
  
          if (!$result) {
              $modulePath = $basePath.'modules'.DIRECTORY_SEPARATOR;
              $moduleDirectory = scandir($modulePath);
              foreach ($moduleDirectory as $dir) {
                  $subModuleDirectory = scandir($modulePath.$dir);
                  foreach ($subModuleDirectory as $entry) {
                      if (is_dir($modulePath.$dir.DIRECTORY_SEPARATOR.$entry)) {
                          $directories = scandir($modulePath.$dir.DIRECTORY_SEPARATOR.$entry);
                          foreach ($directories as $subDir) {
                              if ($modelName == strtolower($subDir)) {
                                  $temp = explode('.', $subDir); // array('User','php')
                                  $result = $temp[0];
                                  break;
                              }
                          }
                      }
                  }
              }
          }
          return $result;
      }
  </code></pre>
  
  <p><strong>My Question is</strong> : Can this cause any performance issues since i am checking this for every API call ? </p>