File 在Yii2中下载PDF文件时出错:未知方法

File 在Yii2中下载PDF文件时出错:未知方法,file,pdf,download,yii2,yii2-advanced-app,File,Pdf,Download,Yii2,Yii2 Advanced App,我想从frontend/web/uploads下载PDF文件。控制器中有一个错误(可能): 调用未知方法:frontend\controllers\BukuController::findModel() 这是我的源代码: BukuController.php public function actionDownload($id) { $model = $this->findModel($id); $file ='../frontend/uploads/'.$model-&g

我想从
frontend/web/uploads
下载PDF文件。控制器中有一个错误(可能):

调用未知方法:
frontend\controllers\BukuController::findModel()

这是我的源代码:

BukuController.php

public function actionDownload($id)
{
    $model = $this->findModel($id);
    $file ='../frontend/uploads/'.$model->file_buku;

    if(file_exists($file))
    {
        return Yii::$app->response->sendFile($file);
        exit;
    }
}
这是视图中的函数,
views/buku/index.php

<?= Html::a('Download', ['download','id'=> $buku->file_buku]); ?>


(已解决)

作为错误状态,您在名为
findModel
的BukuController方法中缺少。 此方法应在数据库中搜索模型。 大概是这样的:

protected function findModel($id)
{
    if (!is_null($model = Buku::findOne($id))) {
        return $model;
    }
    throw new NotFoundHttpException('The requested page does not exist.');
}
当然,如果您的模型不是
Buku
,则需要对其进行相对更改,并将其导入控制器文件的顶部:

use app\models\Buku; // Basic application.
use common\models\Buku; // Advanced application when models store in common folder.
use frontend\models\Buku; // Advanced application when models store in frontend folder.

$file
变量更改为此


$file=Yii::$app->getBasePath()。/web/uploads/。$model->file\u buku

它可以工作!谢谢你。但下载不起作用。没有下载操作。怎么样?请将帮助您解决问题的答案标记为正确。好的,谢谢提醒,不要在问题标题中添加“已解决”: