# Request

Actions such as store and update might need access to the request.

Accessing request params is fairly easy:

public function update(Request $request, $id)
{
    return $request->name;
}
1
2
3
4

Using the get method of the request:

public function update(Request $request, $id)
{
    return $request->get('name');
}
1
2
3
4

Using the request global method:

public function update($id)
{
    return request()->get('name');
}
1
2
3
4

# Request class

The default Request is fairly simple, instead you can create class requests.

A class request extends the default request class:

php tea make:request name=ProductStoreRequest

<?php

namespace App\Http\Requests;

use Devlob\Request\Request;

class ProductStoreRequest extends Request
{
    public function authorize(): bool
    {
        return false;
    }

    public function rules(): array
    {
        return [];
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

authorize will check if the user is authorized to continue.

If authorize returns false then the user receives this error:

{"message": "Authorization failed"}

You can override the message by overriding the authorizationFailed method, add this inside your request class:

public function authorizationFailed()
{
    return json([
        'message' => 'User is not authorized to take this action. Sorry!'
    ], 401);
}
1
2
3
4
5
6

rules will validate your request, more of that here.

You can override the default structure by overriding the validationFailed method, add this inside your request class:

public function validationFailed()
{
    return json([
        'errors'  => $this->getErrors(),
        'message' => 'Oopsies you have some validaton errors!'
    ], 422);
}
1
2
3
4
5
6
7