REST GET with parameter ignored, PHP Symfony 3 Mpd

2020-03-31 06:27发布

问题:

Working on a REST API for PDF processor using Mpdf(and tfox symfony bundle) on Symfony 3 Framework. I created two GET requests, one with no parameters for testing, and one with the parameter(URL of the HTML file) I want to read and then convert into PDF.

The Generic GET function:

  /**
 *
 * @Rest\Get("/create")
 */
public function createPDFAction(){
    $mpdfService = $this->get('tfox.mpdfport');
    $html = "<h1> Hello </h1>";
    $mpdf = $mpdfService->getMpdf();
    $mpdf->WriteHTML($html);
    $mpdf->Output();
    exit;
}

The Second GET function with parameter:

/**
 * @param $htmlSource
 * @Rest\Get("/create/{htmlSource}")
 */
public function createPDFFromSourceAction($htmlSource){
    $mpdfService = $this->get('tfox.mpdfport');
    $html = file_get_contents($htmlSource);
    $mpdf = $mpdfService->getMpdf();
    $mpdf->WriteHTML($html);
    $mpdf->Output();
    exit;
}

The problem is, when I call the second function using browser or Postman the first function is always returned instead and I get the PDF with "Hello", if I remove the first GET function, I get error "no route found for GET/create"

I investigated:

  • The PDF URL is correct, I manually inserted it in first function and worked
  • No syntax error, I copied the same function without the parameters and worked

The Calls I do are:

  • http://localhost:8000/create This one works
  • http://localhost:8000/create?htmlSource=PATH-TO-FILE-LOCALLY This one doesnot work

If I put the PATH-TO-FILE-LOCALLY in function 1 manually it works fine

So I have 2 Questions:

  1. Since I am new to REST and LAMP, should I use GET or others ? My goal is to read the HTML form that the user will fill into a variable and pass it to Mpdf which will convert it into PDF and return that PDF for viewing or download
  2. Why only the first GET function is being read ?

Notes: I am developing on Linux, with PHPStorm, PHP 7, Symfony 3, localhost, the html file I am testing with is on my local machine

Side point: In case this is resolved, I am supposed to upload this to my clients server (which is Apache) - do you have any guides on how to do that and what should be the URLs changed to ?

Thank you all in advance

Updates:

I have changed the functionality to POST methods and it now works fine:

 /**
 * @Rest\Post("/mPDF/")
 */
public function createPDFAction(Request $request){
    $source = $request->get('source');
    if($source == ""){
        return new View('No Data found', Response::HTTP_NO_CONTENT);
    }
    $mpdfService = $this->get('tfox.mpdfport');
    $html = file_get_contents($source);
    $mpdf = $mpdfService->getMpdf();
    $mpdf->WriteHTML($html);
    $mpdf->Output();
    exit;

}

After publishing to Apache production server and some configuration tweaks the site is now live ! - but now I am facing a new issue which I will post a new question for with all the config info I have - basically POST method is returning { "error": { "code": 405, "message": "Method Not Allowed" } }

回答1:

http://localhost:8000/create?htmlSource=PATH-TO-FILE-LOCALLY

("/create/{htmlSource}")

These paths do not match. First path consists of domain name, and route create, while second path has route "create" + slash + wildcard.

Query parameters are not defined within routing annotation. Instead, access them inside controller, using

public function createPDFFromSourceAction(Request $request)
{
    $htmlSource = $request->query->get('htmlSource'); // query string parameter
    $somethingElse = $request->request->get('somethingElse'); //POST request parameter
    ...
}

Symfony will pass Request object inside the controller for you.

As for your other question, GET requests are usually used for things that do not change the state of the application, and POST/PUT/PATCH/DELETE requests change the state. Since you are uploading something, use POST request.

For your 'side note' you should ask another question instead.