master

laravel/framework

Last updated at: 29/12/2023 09:22

Precognition.php

TLDR

The given file, Precognition.php, is a part of the Illuminate/Foundation package in the Demo Projects project. It contains a class named Precognition with a single static method called afterValidationHook(). This method returns a closure that can be used as a hook for precognition requests.


Methods

afterValidationHook

This method is a static method of the Precognition class. It returns a closure that can be used as a hook for precognition requests.

Parameters

  • $request (object) - An instance of Illuminate\Http\Request representing the current request.

Return Value

The method returns a closure that takes a single parameter $validator and uses it along with the $request parameter to perform certain actions. If the validator's messages are empty and the request headers contain the key 'Precognition-Validate-Only', the closure will abort the request with a 204 status code and set the 'Precognition-Success' header to 'true'.


<?php

namespace Illuminate\Foundation;

class Precognition
{
    /**
     * Get the "after" validation hook that can be used for precognition requests.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Closure
     */
    public static function afterValidationHook($request)
    {
        return function ($validator) use ($request) {
            if ($validator->messages()->isEmpty() && $request->headers->has('Precognition-Validate-Only')) {
                abort(204, headers: ['Precognition-Success' => 'true']);
            }
        };
    }
}