Override existing route

Overview

In this guide you'll learn how to override existing Store API routes to add additional data to it.

Prerequisites

As most guides, this guide is also built upon the Plugin base guide, but you don't necessarily need that.
Furthermore you should have a look at our guide about Adding a Store API route, since this guide is built upon it.

Decorating our route

First, we have to create a new class which extends AbstractExampleRoute. In this example we will name it ExampleRouteDecorator.
<plugin root>/src/Core/Content/Example/SalesChannel/ExampleRouteDecorator.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Core\Content\Example\SalesChannel;
4
5
use OpenApi\Annotations as OA;
6
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
7
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
8
use Shopware\Core\Framework\Routing\Annotation\Entity;
9
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
10
use Shopware\Core\System\SalesChannel\SalesChannelContext;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
/**
14
* @Route(defaults={"_routeScope"={"store-api"}})
15
*/
16
class ExampleRouteDecorator extends AbstractExampleRoute
17
{
18
protected EntityRepositoryInterface $exampleRepository;
19
20
private AbstractExampleRoute $decorated;
21
22
public function __construct(EntityRepositoryInterface $exampleRepository, AbstractExampleRoute $exampleRoute)
23
{
24
$this->exampleRepository = $exampleRepository;
25
$this->decorated = $exampleRoute;
26
}
27
28
public function getDecorated(): AbstractExampleRoute
29
{
30
return $this->decorated;
31
}
32
33
/**
34
* @Entity("swag_example")
35
* @OA\Post(
36
* path="/example",
37
* summary="This route can be used to load the swag_example by specific filters",
38
* operationId="readExample",
39
* tags={"Store API", "Example"},
40
* @OA\Parameter(name="Api-Basic-Parameters"),
41
* @OA\Response(
42
* response="200",
43
* description="",
44
* @OA\JsonContent(type="object",
45
* @OA\Property(
46
* property="total",
47
* type="integer",
48
* description="Total amount"
49
* ),
50
* @OA\Property(
51
* property="aggregations",
52
* type="object",
53
* description="aggregation result"
54
* ),
55
* @OA\Property(
56
* property="elements",
57
* type="array",
58
* @OA\Items(ref="#/components/schemas/swag_example_flat")
59
* )
60
* )
61
* )
62
* )
63
* @Route("/store-api/example", name="store-api.example.search", methods={"GET", "POST"})
64
*/
65
public function load(Criteria $criteria, SalesChannelContext $context): ExampleRouteResponse
66
{
67
// do some custom stuff here
68
69
return new ExampleRouteResponse($this->exampleRepository->search($criteria, $context->getContext()));
70
}
71
}
Copied!
As you can see, our decorated route has to extend from the AbstractExampleRoute and the constructor has to accept an instance of AbstractExampleRoute. Furthermore, the getDecorated() function has to return the decorated route passed into the constructor. Now we can add some additional data in the load method, which we can retrieve with the criteria.

Registering route

Last, we have to register the decorated route to the DI-container. The ExampleRouteDecorator has to be registered after the ExampleRoute with the attribute decorated which points to the ExampleRoute. For the second argument we have to use the ExampleRouteDecorator.inner.
<plugin root>/src/Resources/config/services.xml
1
<?xml version="1.0" ?>
2
3
<container xmlns="http://symfony.com/schema/dic/services"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6
7
<services>
8
...
9
10
<service id="Swag\BasicExample\Core\Content\Example\SalesChannel\ExampleRouteDecorator" decorates="Swag\BasicExample\Core\Content\Example\SalesChannel\ExampleRoute" public="true">
11
<argument type="service" id="swag_example.repository"/>
12
<argument type="service" id="Swag\BasicExample\Core\Content\Example\SalesChannel\ExampleRouteDecorator.inner"/>
13
</service>
14
</services>
15
</container>
Copied!