忽略参数的REST GET,PHP Symfony 3 Mpdf [英] REST GET with parameter ignored, PHP Symfony 3 Mpdf

查看:96
本文介绍了忽略参数的REST GET,PHP Symfony 3 Mpdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Symfony 3 Framework上使用Mpdf(和tfox symfony捆绑包)开发用于PDF处理器的REST API.我创建了两个GET请求,一个不带测试参数,一个带有要读取的参数(HTML文件的URL),然后转换为PDF.

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.

通用GET函数:

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

第二个GET函数,其参数为:

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;
}

问题是,当我使用浏览器或Postman调用第二个函数时,总是返回第一个函数,并且得到带有"Hello"的PDF,如果删除第一个GET函数,则会出现错误找不到路由" GET/create"

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"

我调查了

  • PDF URL是正确的,我在第一个函数中手动将其插入并正常工作
  • 没有语法错误,我复制了不带参数的相同函数并开始工作

我打的电话是:

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

如果我手动将PATH-TO-FILE-LOCALLY放在函数1中,它会很好地工作

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

所以我有2个问题:

  1. 由于我是REST和LAMP的新手,我应该使用GET还是其他工具?我的目标是阅读用户将填写到变量中的HTML表单,并将其传递给Mpdf,后者会将其转换为PDF并返回该PDF以供查看或下载
  2. 为什么只读取第一个GET函数?

注意:我正在Linux上使用PHPStorm,PHP 7,Symfony 3,本地主机进行开发,正在测试的html文件位于我的本地计算机上

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

要点:如果解决了这个问题,我应该将其上传到我的客户服务器(Apache)上-您是否有关于如何执行此操作的指南,URL应该更改为什么?

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 ?

谢谢大家

更新:

我已将功能更改为POST方法,现在可以正常工作了:

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;

}

发布到Apache生产服务器并进行了一些配置调整后,该站点现在可以正常使用! -但是现在我面临着一个新问题,我将针对我拥有的所有配置信息发布一个新问题-基本上POST方法返回{ "error": { "code": 405, "message": "Method Not Allowed" } }

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" } }

推荐答案

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

("/create/{htmlSource}")

("/create/{htmlSource}")

这些路径不匹配. 第一个路径包含域名和路由create,而第二个路径包含路由创建" +斜杠+通配符.

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将为您传递控制器内的Request对象.

Symfony will pass Request object inside the controller for you.

关于其他问题,GET请求通常用于不更改应用程序状态的事物,而POST/PUT/PATCH/DELETE请求更改状态.由于您正在上传内容,因此请使用POST请求.

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.

这篇关于忽略参数的REST GET,PHP Symfony 3 Mpdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆