Skip to content

Configuration

Wes Rice edited this page Jan 11, 2016 · 10 revisions

First, ensure that craft/config/httpmessages.php exists. All routes that need to utilize Http Messages will be defined in this file.

Routes Configuration

Below is an example of a configured route.

[
    'pattern' => '<uri path pattern>',
    'methods' => [
        '<http method>' => [
            'middleware' => [
                '<middleware handle>' => [
                    '<config key>' => '<config value>',
                ],
            ],
        ],
    ],
],

The routes key of the config array should be an array of associative arrays. Each associative array will define the configuration of that route. Below is the configuration of a single route.

The pattern attribute's value is used by the router to see if the requested path matches.

The methods attribute defines what methods are allowed for the defined pattern. Each method is defined as a key, with the value being an associative array that defines a configuration for that specific route.

The middleware will be executed in the order of the middleware configurations found in the middleware key. Each middleware should also have an associative assigned to it's <middleware handle>. This array will act as a config specific to that middleware.

Example httpmessages.php Config File

<?php

return [

    'routes' => [

        [
            'pattern' => '/api/{elementType:Entry}[/{elementId}]',
            'methods' => [
                'GET' => [
                    'middleware' => [
                        'fractal' => [
                            'transformer' => 'HttpMessagesFractalMiddleware\\Transformers\\EntryTransformer',
                        ],
                        'rest' => [],
                    ],
                ],
                'POST' => [
                    'middleware' => [
                        'fractal' => [
                            'transformer' => 'HttpMessagesFractalMiddleware\\Transformers\\EntryTransformer',
                        ],
                        'rest' => [
                            'validator' => 'HttpMessagesRestMiddleware\\Validators\\EntryValidator',
                        ],
                    ],
                ],
            ],
        ],

        [
            'pattern' => '/api/{elementType:User}[/{elementId}]',
            'methods' => [
                'GET' => [
                    'middleware' => [
                        'fractal' => [
                            'transformer' => 'HttpMessagesFractalMiddleware\\Transformers\\UserTransformer',
                        ],
                        'rest' => [],
                    ],
                ],
                'POST' => [
                    'middleware' => [
                        'fractal' => [
                            'transformer' => 'HttpMessagesFractalMiddleware\\Transformers\\UserTransformer',
                        ],
                        'rest' => [
                            'validator' => 'HttpMessagesRestMiddleware\\Validators\\UserValidator',
                        ],
                    ],
                ],
            ],
        ],

    ],

];

Clone this wiki locally