Skip to main content

Routes

By design, Pandawa takes care most of the handling of any routes. It leverages the route configuration using YAML files to simplify the creation of a route, for example:

# Api/Resources/routes/routes.yaml
products:
type: group
children: _product.yaml

# Api/Resources/routes/_product.yaml
products:
type: resource
path: products
rules: [manage_product]

Typesโ€‹

There is some core types for different purposes to define a route.

groupโ€‹

This type is self-explanatory, it groups different routes together. This type usually is used for a specific route group or route children.

Example ๐Ÿงช
products:
type: group
children: _product.yaml

resourceโ€‹

This acts the same like Laravel's resource controller. You don't have to write your implementation yourself since Pandawa already did the hard-lifting.

Example ๐Ÿงช
products:
type: resource
path: products

messageโ€‹

This one is little bit complex and different from the rest. This type will refer an handler (command/query/etc.) and invoke it.

Example ๐Ÿงช
create_product:
type: message
message: create-product
path: products
methods: [post]

Read more

presenterโ€‹

As for this one, it's the same as route handler you usually do in vanilla Laravel. You'll have to manually write the route implementation and things. It's all in your hand...

Example ๐Ÿงช
product_image:
type: presenter
presenter: image
path: products/{product}/image
methods: [get]

Validationโ€‹

Declaring a rule is easy as smashing a cardboard wall ๐Ÿ˜ƒ

Declaring a ruleโ€‹

  1. Make a YAML file in Resources/rules of any folder inside your app src.

  2. Using the following template, feel free to fill it to your use-case.

    rule_name:
    constraints:
    key: "required|string"
    key2: "required|integer"

    Constraints values are the same as Laravel's validation types. This doesn't only apply to the request body but also includes URL parameter, query, etc.

Using the ruleโ€‹

All you need to add a single line to your route configuration

rules: [rule_name]
Example ๐Ÿงช
create_product:
type: message
message: create-product
path: products
methods: [post]
rules: [manage_product]

Middlewareโ€‹

Using middlewares in Pandawa is also a piece of cake. All you need to do is make the middleware implementation exactly the same as vanilla Laravel middleware and register it at app/laravel/Http/Kernel.php, in the $routeMiddleware option.

Example ๐Ÿงช
// app/laravel/Http/Kernel.php

// ...
protected $routeMiddleware = [
// ...
'roles.has' => \Pandawa\Api\Http\Middleware\HasRole::class,
];

Using the middlewareโ€‹

All you need to add a single line to your route configuration like rules

middlewares: [middleware_name]
Example ๐Ÿงช
create_product:
type: message
message: create-product
path: products
methods: [post]
rules: [manage_product]
middlewares: ["roles.has:admin"]