I need to create a middleware app that provides an api and consumes two apis. However, I am having a problem with two Symfony packages: HTTP Client and HttpFoundation
I don't understand when I should be using one versus the other. Little new to Symfony and I've read the docs but they both allow for you to consume and provide and api vis request and response methods or classes depending on which package you use.
I know I'm missing something obvious but I'm having a hard time of it. Any help would be greatly appreciated.
You should have to use The HttpFoundation Component. It is easy to use in your Symfony application controller too. i.e
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* #Route("/api/product/list", name="api_product_list", methods={"GET"})
* #param Request $request
* #return Response
*/
public function getList(Request $request): Response
{
$product = $request->get('product');
return $this->json(['product' => $product]);
}
More information about HTTPFoundation you already know.
Related
In Symfony you can do :
use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();
I'd like to know if there is something similar in TYPO3.
Thank you.
See this change https://review.typo3.org/#/c/40355/
[FEATURE] Introduce Request/Response based on PSR-7
The PSR-7 standard is adapted into the TYPO3 Bootstrap with a
backwards-compatible layer.
The PSR-7 implementation brings several new classes: * Message (the
base for Requests and Responses) * Request (for Requests made within
PHP) * ServerRequest and a factory based on the current system
environment * Response * Uri (a unified API for fetching several
parts of an URI)
At any TYPO3 request a new ServerRequest object is created inside the
Bootstrap and handed over to the RequestHandler which can then use
this object for checking certain GET and POST variables instead of
using GeneralUtility.
The proper call (usually a Controller) creates a Response object that
is handed back to the RequestHandler + Bootstrap. The TYPO3 Bootstrap
will output anything related in the shutdown() method.
An example is shown with the LoginController and currently hard-wired
as no proper routing/dispatching is there yet.
Currently this is an internal API as the rest (Dispatch/Router and
Controller API) will follow once the base is in.
Please note that the PSR-7 standard works with Value Objects meaning
that it is not possible to modify any object but instead new objects
will be created for Message, ServerRequest and Response if modified.
The next steps are:
* Integrate proper Routing + Dispatching for Backend Routes to register new BE requests
* Migrate all AJAX Calls to use the new API and request / response handling
* Introduce a common Base Controller for all regular BE requests which is based on Request/Response and works as a replacement for sc_base
* Then: proper documentation for the whole bootstrap / dispatch + routing / controller logic
* Integrate symfony console app into the CLI Bootstrap as alternative for Request/Response
* Refactor TSFE to use Response / Request objects properly
* Refactor redirects logic to use Response objects
see RequestHandler in EXT:backend/Classes/Http/ and EXT:frontend/Classes/Http for the usages in the core
I need to set up several parameters after user authentication.
We have a db with with oauth2 clients.
When one of them got access token and is trying to get access to protected API I need to identify the client (which is easy since access_token is bound to particular client) and define several application parameters (actually I need to load a specific file parameters.yml depends on the client).
My questions is:
How can I hook to event when user is authenticated?
How can I load a particular parameters.yml and make it relevant after user authenticate ?
Thank you!
When user is successfully authenticated, you can write a listener which listens to SecurityAuthenticationEvents::AUTHENTICATION_SUCCESS.
The public method of the listener should look like this:
public function onAuthenticationSuccess(AuthenticationEvent $event)
{
/**
* #var User $user
*/
$user = $event->getAuthenticationToken()->getUser();
// ...
return;
}
I believe you can use YamlFileLoader for that. Of course you can create a service class to read the contents from yaml files and provide them to different services in your app. I would not try to mix them with regular parameter / config files.
Playing around with symfony. I have two bundles and each bundle has a controller within it. Just to see how routing works I gave the same path to functions within both controllers. Bundle B was the newly created bundle and when the URL app/simple was hit I got a response from bundle B always. Just curious as to whether there is any logic behind this.
PS: I know this is bad practice but just wanted to see how the guts of routing in works.
/**
* #Route("/app/simple", name="homepage")
*/
public function indexAction()
{
return new Response('Hello From bundle A!');
}
In bundle B
/**
* #Route("/app/simple", name="homepage")
*/
public function indexAction()
{
return new Response('Hello From bundle B!');
}
Your app has a single routing configuration which can include other configurations. Probably app/config/routing.yml.
That configuration file will include the routes for your bundles by using the resource key that can import routes from another routing.yml file or from annotations in a PHP controller.
The order of those will determine which route gets chosen since Symfony2 always uses the first matching route.
I have an application that relies on third party services. In order to make sure that the application works properly, I want to mock the third party services and make sure that the application is working as expected.
This requires that I am able to configure the mock services before creating the requests. However, I am unable to do so.
Consider the following code:
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
//..
class MyTest extends WebTestCase
{
public function testSignupLink()
{
$container = static::createClient()->getContainer();
// This returns a different instance from the one used by the client request
$service = $container->get('third-party-service');
$service->setErrorState(MockService::SOME_ERROR_STATE);
// ...
// The request creates a new instance of the $service internally which doesn't have the error state that was set above
$client->request('POST', '/abc/1');
}
}
The 'abc' controller relies on a service that I can't access. When I access the service from the container, I get a different instance from the one that is used by the client request.
Is there any way to handle this?
If I correctly understood you, here is what you need:
https://github.com/PolishSymfonyCommunity/SymfonyMockerContainer
I have a Symfony2 bundle which I want to use database table which stores key value configuration parameters. I want to be able to load a query and cache it for a long time and be able to inject the configuration parameters into symfony2 service container.
Right now I am injecting a service which loads the configuration from doctrine, and calling a get($key) method to retrieve the value for the key I want.
I basically want these configuration options to be available from the symfony2 service container parameter bag.
Is there maybe an event I could tie into or some sort of compiler pass I can use with my bundle to achieve this?
I'll do something like that in your service listener
public function onLateKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$mydata= $this->manager->getRepository('YourBundle:YourTable')->getAll();
$parameters['mydata'] = $mydata;
$request->attributes->add($parameters);
}
In your Controller, you can get your parameters :
$this->container->get('request')->attributes->get('mydata');