How to set filename of pdf in anonymous function as parameter

Hi guys, my intention is to get content of pdf file,which has already been uploaded in alias folder. For this, i programmed a method in Controller like this





    public function actionFile($filename) {

        $storagePath = Yii::getAlias('@pictures');

        if (!preg_match('/^[a-z0-9]+\.[a-z0-9]+$/i', $filename) || !is_file("$storagePath/$filename")) {

            throw new \yii\base\NotSupportedException ('Die PDF-Datei existiert nicht!');

        }

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

    }



I fail calling this method in Controller using link in anonymous function. It will throw out error:





Unknown Property – yii\base\UnknownPropertyException

Getting unknown property: frontend\modules\dateianhang\models\Dateianhang::ERMTheorie.pdf



Any ideas, how to implement link correctly. Here is link in GridView:





    [

        'class' => 'yii\grid\ActionColumn',

        'template' => '{anlagen_index} <br>{anlagen_create} ',

        'buttons' => [

            'anlagen_index' => function ($model, $id) {

            $filename="ERMTheorie.pdf";

                return Html::a('<span class="glyphicon glyphicon-paperclip"></span>', ['/dateianhang/dateianhang/file','id' => $id->$filename], ['title' => 'Anlagen anzeigen', 'data' => ['pjax' => '0']]);

/*return Html::a('<span class="glyphicon glyphicon-paperclip"></span>', ['/dateianhang/dateianhang/file','filename' => $id->$filename], ['title' => 'Anlagen anzeigen', 'data' => ['pjax' => '0']]);

this will throw out 'Try to get property of non-object' without $model as parameter in function




            },            },

        ],

    ],



why don’t you put the file name directly into the array


'anlagen_index' => function ($model, $id) {


                return Html::a(

                              '<span class="glyphicon glyphicon-paperclip"></span>', 

                              ['/dateianhang/dateianhang/file','filename' => "ERMTheorie.pdf"], 

                              ['title' => 'Anlagen anzeigen', 'data' => ['pjax' => '0']]

                );

            },

Update: also the callback signature is following


function ($url, $model) {

    // return the button HTML code

}

Got it by my own…





  [

        'class' => 'yii\grid\ActionColumn',

        'template' => '{anlagen_index} <br>{anlagen_create} ',

        'buttons' => [

            'anlagen_index' => function () {

                $filename = "ERMTheorie.pdf";

                return Html::a('<span class="glyphicon glyphicon-paperclip"></span>', ['/dateianhang/dateianhang/file', 'filename' => $filename], ['title' => 'Anlagen anzeigen', 'data' => ['pjax' => '0']]);

            },

        ],

    ],



But Controller seems to be faulty!




var_dump("$storagePath/$filename");



will show





C:\xampp\htdocs\yii2_perswitch\frontend\modules\dateianhang\controllers\DateianhangController.php:262:

string 'C:\xampp\htdocs\yii2_perswitch\frontend/web/pictures/ERMTheorie.pdf' (length=67)



I know for 100 per cent, that file called ‘ERMTheorie.pdf’ is in my alias folder

Anyway,I will get Exception. Any ideas,what is wrong with controller?

My fault. Alias was wrong!

Alias correctly is like this,of course:




$storagePath = Yii::getAlias('@documents');  



So,this thread can be closed as succesfully solved!