Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Cqs Routing Laravel Package

digital-craftsman/cqs-routing

View on GitHub
Deep Wiki
Context7

Request decoder examples

Interface

interface RequestDecoder
{
    public function decodeRequest(Request $request): array;
}

See position in process

JSON request decoder

⭐ This request decoder is supplied with the bundle.

The simple implementation of a request decoder is the JsonRequestDecoder:

final readonly class JsonRequestDecoder implements RequestDecoder
{
    public function decodeRequest(Request $request): array
    {
        /** [@var](https://github.com/var) string $content */
        $content = $request->getContent();

        return (array) json_decode($content, true, 512, JSON_THROW_ON_ERROR);
    }
}

When feeding it a request with the following JSON body:

{
    "userId": "5118d20f-0ca5-40d1-99a8-1d1c20d675d6",
    "emailAddress": "user@example.com",
    "name": {
        "firstName": "John",
        "lastName": "Doe"
    },
    "password": "V6nP2mKmxn42Km3JG3x@"
}

The request decoder will return the following array:

[
    'userId' => '5118d20f-0ca5-40d1-99a8-1d1c20d675d6',
    'emailAddress' => 'user@example.com',
    'name' => [
        'firstName' => 'John',
        'lastName' => 'Doe',
    ],
    'password' => 'V6nP2mKmxn42Km3JG3x@',
]

Request decoder for GET parameter and security context

Another use case would be accessing data outside the body. This is needed when we want to get an image through a query that is triggered by the browser when used in an img tag. We obviously can't add any custom body to that request. With the request decoder we can pull information about the user from the security context and add additional information from the GET parameters.

final readonly class UserImageRequestDecoder implements RequestDecoder
{
    public function __construct(
        private Security $security,
    ) {
    }

    public function decodeRequest(Request $request): array
    {
        /** [@var](https://github.com/var) User $user */
        $user = $this->security->getUser();

        $imageName = $request->get('imageName');

        return [
            'userId' => (string) $user->id,
            'imageName' => $imageName,
        ];
    }
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle