title: YII2 rest api url call model method
body: <p>Im kinda new to rest api, especially with YII2.</p>
  
  <p>I have three tables: <code>students(id, name)</code>, <code>courses(id, subject)</code> and <code>students_courses(student_id, course_id)</code>. Many to many relation.</p>
  
  <p>I need to get students id, name and courses via relation table.</p>
  
  <pre><code>myexample.com/api/v1/students
  </code></pre>
  
  <p>gives me json with <code>id</code> and <code>name</code> only</p>
  
  <pre><code>myexample.com/api/v1/students/get-students-courses
  </code></pre>
  
  <p>gives me 404 not found</p>
  
  <p>Here's my model:</p>
  
  <pre><code>&lt;?php
  
  namespace app\models;
  
  use Yii;
  
  class Students extends \yii\db\ActiveRecord
  {
  
      public static function tableName()
      {
          return 'students';
      }
  
  
      public function rules()
      {
          return [
              [['name'], 'required'],
              [['name'], 'string', 'max' =&gt; 255]
          ];
      }
  
      public function attributeLabels()
      {
          return [
              'id' =&gt; 'ID',
              'name' =&gt; 'Name',
          ];
      }
  
      public function getStudentsCourses()
      {
          return $this-&gt;hasMany(StudentsCourses::className(), ['student_id' =&gt; 'id']);
      }
  }
  </code></pre>
  
  <p>and urlManager configuration:</p>
  
  <pre><code>'urlManager' =&gt; [
          'enablePrettyUrl' =&gt; true,
          'enableStrictParsing' =&gt; true,
          'showScriptName' =&gt; false,
          'rules' =&gt; [
              ['class' =&gt; 'yii\rest\UrlRule', 'controller' =&gt; ['v1/students']],
          ],
      ],
  </code></pre>
  
  <p><strong>EDIT:</strong>
  Thanks, Salem Ouerdani, this worked.</p>
  
  <pre><code>public function getCourses()
  {
      return $this-&gt;hasMany(Courses::className(), ['id' =&gt; 'course_id'])
      -&gt;viaTable(StudentsCourses::tableName(), ['student_id' =&gt; 'id'])
      -&gt;all();
  }
  
  public function extraFields()
  {
      return ['studentsCourses' =&gt; function(){
          return $this-&gt;getCourses();
      }];
  }
  </code></pre>