V
V
Vasily Bratushka2015-02-24 13:15:40
symfony
Vasily Bratushka, 2015-02-24 13:15:40

How to push JSON into Response?

The root of the question lies here stackoverflow.com/questions/28683727/submit-form-w... .
The bottom line is that you can't use $form->handleRequest($request), because The JSON is not in the $request object, but is available via $request->getContent() .
Actually the question is: how to competently shove JSON into $request without using crutches?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alex, 2015-02-24
@shoomyst

$request = $request->duplicate(null, json_decode($request->getContent()));

But that's probably not the best option.

T
Twist, 2015-02-25
@bboytiwst

Today I helped a friend with Silex "ohm and there is such a thing

$app->before(function (Request $request) {
    if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
        $data = json_decode($request->getContent(), true);
        $request->request->replace(is_array($data) ? $data : array());
    }
});

this code is very easy to port to symfony by simply registering a listener symfony.com/doc/current/cookbook/service_container ... post https://github.com/qandidate-labs/symfony-json-req... - listener

V
Vasily Bratushka, 2015-02-25
@emigrant90

in theory, this can be entered directly into the Request class, here in this function

public static function createFromGlobals()
    {
        // With the php's bug #66606, the php's built-in web server
        // stores the Content-Type and Content-Length header values in
        // HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH fields.
        $server = $_SERVER;
        if ('cli-server' === php_sapi_name()) {
            if (array_key_exists('HTTP_CONTENT_LENGTH', $_SERVER)) {
                $server['CONTENT_LENGTH'] = $_SERVER['HTTP_CONTENT_LENGTH'];
            }
            if (array_key_exists('HTTP_CONTENT_TYPE', $_SERVER)) {
                $server['CONTENT_TYPE'] = $_SERVER['HTTP_CONTENT_TYPE'];
            }
        }

        $request = self::createRequestFromFactory($_GET, $_POST, array(), $_COOKIE, $_FILES, $server);

        if (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
            && in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), array('PUT', 'DELETE', 'PATCH'))
        ) {
            parse_str($request->getContent(), $data);
            $request->request = new ParameterBag($data);
        }

        return $request;
    }

Forkney Symphony vendor bundle, maybe they will approve, you will have a plus in karma the size of a liner)

T
Timur Mukhtarov, 2015-04-08
@fearintino

How about this option:

$content = $this->get("request")->getContent();
$params = json_decode($content, true);
$request->request->add($params);

R
Rsa97, 2017-11-13
@Veneomin

For a start - to write down request more clearly. In ON conditions, write the fields of the attached table in the left parts of the comparisons:

SELECT * 
  FROM `tbl_1` 
  LEFT JOIN `tbl_2` ON `tbl_2`.`id_fp` = `tbl_1`.`foto_id` AND `tbl_2`.`sklad` = 'Склад' 
  WHERE (deleted="no" AND `where`="arh")  AND (`type`="Cloth" ) 
  ORDER BY `foto_id` ASC 
  LIMIT 9447,20;

Now it is clear that to speed up JOIN, it is worth making a composite index (`id_fp`, `sklad`) in the `tbl_2` table.
Then you need to look at EXPLAIN and possibly move the conditions from WHERE to the prefetch from `tbl_1`.
But, if several lines from `tbl_2` are attached to one line from `tbl_1`, then ORDER BY and LIMIT must be left outside.
SELECT * 
  FROM (
    SELECT *
      FROM `tbl_1` 
      WHERE (deleted="no" AND `where`="arh")  AND (`type`="Cloth" ) 
      ORDER BY `foto_id` ASC 
      LIMIT 9447,20
  ) AS `t1`
  LEFT JOIN `tbl_2` ON `tbl_2`.`id_fp` = `t1`.`foto_id` AND `tbl_2`.`sklad` = 'Склад'

And, finally, replace * with the required list of fields, so as not to pull unnecessary data from the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question