Symfony 4: Attempted to call an undefined method named "getParameter" of class "SymfonyComponentDependencyInjectionArgumentServiceLocator"

  controller, php, symfony, symfony4

I’m trying to make a function to download pdf files based on pdf id stored in the database. Files are stored in public/CVs folder and I’m getting the following error Attempted to call an undefined method named "getParameter" of class "SymfonyComponentDependencyInjectionArgumentServiceLocator". Here is the code:

/**
 * @Route("/home/download/{id}")
 */
public function download($id) {
    try {
        $cv = $this->getDoctrine ()->getRepository (JobApplications::class)->find ( $id );
        if (! $cv) {
            $array = array (
                'status' => 0,
                'message' => 'File does not exist'
            );
            return new JsonResponse ( $array, 200 );
        }
        $fileName = $cv->getfilename ();
        $file_with_path = $this->container->getParameter( 'kernel.project_dir' ) . "/public/CVs/" . $fileName;
        $response = new BinaryFileResponse ( $file_with_path );
        $response->headers->set ( 'Content-Type', 'application/pdf' );
        $response->setContentDisposition ( ResponseHeaderBag::DISPOSITION_ATTACHMENT, $fileName );
        return $response;
    } catch ( Exception $e ) {
        $array = array (
            'status' => 0,
            'message' => 'Download error'
        );
        return new JsonResponse ( $array, 400 );
    }
}

Source: Ask PHP

LEAVE A COMMENT