How to override the view action in the ActiveController

Is there any way to override the view action in the ActiveController by just creating a function in the controller (not creating a new class)?

I have a controller called MediaController. When the user sends a GET request to .../api/media/xyz.jpg I want to send the file xyz.jpg to the user

Actually I need something like this:




 public function actionView($filename) {

        

    $mediaPath = \Yii::getAlias('@mediaPath');

    return Yii::$app->response->sendFile("$storagePath/$filename", $filename);

  }



but it’s not working.

By the way when I run ../api/1.jpg (.jpg at the end of url) I get InvalidRouteException. Why?

Probably not a good idea to use http://www.yiiframework.com/doc-2.0/yii-web-response.html#sendFile()-detail within your REST API method (the method is supposed to send the content to the browser). I think it’s better to send a response like (just an example to catch the idea):




    public function actionView($filename) {

       return [

           'content-type' => 'jpg',

           'content' => base64_encode(file_get_contents($filename))

       ];

    }