Php 从Yii 2中的其他控制器访问控制器

Php 从Yii 2中的其他控制器访问控制器,php,yii2,Php,Yii2,有没有办法从FirstController访问函数到SecondController?如果有,请有人给我指路。 第一控制器: 第二控制器: 您可以这样重构代码 第一控制器: 第二控制器: 下载程序:在@app/models中 非常感谢。这很有帮助 class FirstController extends Controller { public function actionDownload($fileName) { $path = 'templates/'.$

有没有办法从FirstController访问函数到SecondController?如果有,请有人给我指路。 第一控制器:

第二控制器:


您可以这样重构代码

第一控制器:

第二控制器:

下载程序:在@app/models中


非常感谢。这很有帮助
class FirstController extends Controller
{
    public function actionDownload($fileName)
    {
        $path = 'templates/'.$fileName;
        if(file_exists($path)){
            return Yii::$app->response->sendFile($path);
        }
    }
}
class SecondController extends Controller
{
    public function actionView($id)
    {

        // i want to access function download() from the FirstController here

        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
}
use app\models\Downloader;

class FirstController extends Controller
{
    public function actionDownload($fileName)
    {
        Downloader::download($filename);
    }
}
use app\models\Downloader;

class SecondController extends Controller
{
    public function actionView($id)
    {
        $filename = "file"; // set this as you wish

        Downloader::download($filename);

        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
}
namespace app\models;

class Downloader
{
    public static function download(string $filename)
    {
        $path = 'templates/'.$fileName;
        if (file_exists($path)) {
            return Yii::$app->response->sendFile($path);
        }
    }
}