Question

Is there any special way a function (like converting a given string to an array) should be declared in Symfony2? Please keep in mind that the function is only meant to be used in one class, so Dependency Injection does not do the job. What I personally like is to not put it directly in the controller as a extra function, but put it into a class, so it fits the separation of concerns.

Redacted example

I would have something like this:

<?php
// ...
public function someAction(Request $request)
{
    $query = $request->query;

    // Do something with this data
    $data['somedata']['hello'] = $query->get('a');
    $data['someohterdata']['goodbye'] = $query->get('b');
    $data['c'] = $query->get('c');

    $response = new JsonResponse();

    $response->setData($data);
    return $response;
}

And what I'm trying to accomplish is something like this

<?php

public function someAction(Request $request)
{
    $query = $request->query;

    // Access something outside this class
    $outsideClass = ...;
    $data = $outsideClass->createDataFromQuery($query);

    $response = new JsonResponse();

    $response->setData($data);
}

Not sure what is best practice, so asking it here.

Was it helpful?

Solution

There is a simple:

public function someAction(Request $request)
{
    $query = $request->query;

    // Access something outside this class
    $data = $query;

    $response = new JsonResponse();

    $response->setData($data);
}

It's a normally, if you want to use dynamic response

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top