Symfony将参数传递给使用GET Type的方法

时间:2018-10-11 08:35:19

标签: php symfony get parameter-passing internal-server-error

我正在使用php symfony 3.4版-GET方法来调用带有参数的方法,但出现以下错误。

service call : http://localhost/myworkspace/AMSCMS/web/app_dev.php/sse/server/events/get?data=1 

method :
/**
     * @Rest\Get("/server/events/get")
     */
    public function getServerEventsGetAction($dt) {
        try {   
            if($dt == 1){
                header('Content-Type: text/event-stream');
                header('Cache-Control: no-cache');
                $time = date('r');
                echo "data: The server time is: {$time}\n\n";
                flush();    
            }                   
        } catch ( Exception $ex ) {
            $result = $display->getJsonException($ex);
        }

    }

Error :
    {"error":{"code":500,"message":"Internal Server Error","exception":[{"message":"Controller \"AppBundle\\Controller\\ServerEventsController::getServerEventsGetAction()\" requires that you provide a value for the \"$dt\" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.","class":"RuntimeException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"C:\\xampp\\htdocs\\myworkspace\\ApartmentManagementSystem\\ApartmentManagementScms\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver.php","line":78,"args":[]},{"namespace":"Symfony\\Component\\HttpKernel\\Controller","short_class":"ArgumentResolver","class":"Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver","type":"->","function":"getArguments","file":"C:\\xampp\\htdocs\\myworkspace\\ApartmentManagementSystem\\ApartmentManagementScms\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpKernel\\Controller\\TraceableArgumentResolver.php","line":38,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["array",[["object","AppBundle\\Controller\\ServerEventsController"],["string","getServerEventsGetAction"]]]]},{"namespace":"Symfony\\Component\\HttpKernel\\Controller","short_class":"TraceableArgumentResolver","class":"Symfony\\Component\\HttpKernel\\Controller\\TraceableArgumentResolver","type":"->","function":"getArguments","file":"C:\\xampp\\htdocs\\myworkspace\\ApartmentManagementSystem\\ApartmentManagementScms\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpKernel\\HttpKernel.php","line":143,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["array",[["object","AppBundle\\Controller\\ServerEventsController"],["string","getServerEventsGetAction"]]]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"HttpKernel","class":"Symfony\\Component\\HttpKernel\\HttpKernel","type":"->","function":"handleRaw","file":"C:\\xampp\\htdocs\\myworkspace\\ApartmentManagementSystem\\ApartmentManagementScms\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpKernel\\HttpKernel.php","line":68,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["integer",1]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"HttpKernel","class":"Symfony\\Component\\HttpKernel\\HttpKernel","type":"->","function":"handle","file":"C:\\xampp\\htdocs\\myworkspace\\ApartmentManagementSystem\\ApartmentManagementScms\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpKernel\\Kernel.php","line":200,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["integer",1],["boolean",true]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"Kernel","class":"Symfony\\Component\\HttpKernel\\Kernel","type":"->","function":"handle","file":"C:\\xampp\\htdocs\\myworkspace\\ApartmentManagementSystem\\ApartmentManagementScms\\web\\app_dev.php","line":29,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"]]}]}]}}

1 个答案:

答案 0 :(得分:1)

您没有将GET参数作为操作参数,而是需要通过Request对象获得这些参数:

public function getServerEventsGetAction(Request $request) {
    $dt = $request->get('data');
    // ... check that $data is not null etc
}

或者,您也可以更改要包含在url中的参数(而不是GET参数),这样您就可以像操作参数一样正常访问它,就像控制器一样:

/**
 * @Rest\Get("/server/events/get/{dt}")
 */

并使用以下格式的网址:

http://localhost/myworkspace/AMSCMS/web/app_dev.php/sse/server/events/get/1