The Wayback Machine - https://web.archive.org/web/20221117094857/https://github.com/laravel/docs/pull/8261
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Precognition #8261

Open
wants to merge 68 commits into
base: 9.x
Choose a base branch
from
Open

[9.x] Precognition #8261

wants to merge 68 commits into from

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Sep 29, 2022

@timacdonald timacdonald marked this pull request as ready for review Oct 4, 2022
<a name="executing-controller"></a>
### Executing Code In A Controller

As previously discussed, Laravel Precognition with not invoke the controller, however many applications may not be using route model binding, form requests, and middleware and instead are doing this work in the controller. This is why we have created a mechanism for applications that want to use Precognition but also keep their code co-located within the controller. Imagine we have the following controller:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
As previously discussed, Laravel Precognition with not invoke the controller, however many applications may not be using route model binding, form requests, and middleware and instead are doing this work in the controller. This is why we have created a mechanism for applications that want to use Precognition but also keep their code co-located within the controller. Imagine we have the following controller:
As previously discussed, Laravel Precognition will not invoke the controller, however many applications may not be using route model binding, form requests, and middleware and instead are doing this work in the controller. This is why we have created a mechanism for applications that want to use Precognition but also keep their code co-located within the controller. Imagine we have the following controller:

@phh
Copy link
Contributor

phh commented Oct 11, 2022

@timacdonald This looks awesome! We hope to adopt this as soon as possible! Any plans to when to release the npm packages - or if you would make the repositories public while they're getting worked on? 🙏

@mreduar
Copy link

mreduar commented Oct 12, 2022

@timacdonald This looks awesome! We hope to adopt this as soon as possible! Any plans to when to release the npm packages - or if you would make the repositories public while they're getting worked on? 🙏

I would also love to try it, I have been following the PR's since Laracon 😅

<a name="handling-precognitive-requests"></a>
### Handling Precognitive Requests

Precognitive requests should generally be side-effect free. This is where the Precognition "pattern" comes in. It is recommend when you add Precognition to your routes that you consider the side-effects triggered in your application's middleware and form requests. If the side-effects present should be skipped for precognitive requests, you may need to add a conditional around the side-effect.
Copy link

@mreduar mreduar Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Precognitive requests should generally be side-effect free. This is where the Precognition "pattern" comes in. It is recommend when you add Precognition to your routes that you consider the side-effects triggered in your application's middleware and form requests. If the side-effects present should be skipped for precognitive requests, you may need to add a conditional around the side-effect.
Precognitive requests should generally be side-effect free. This is where the Precognition "pattern" comes in. It is recommended when you add Precognition to your routes that you consider the side-effects triggered in your application's middleware and form requests. If the side-effects present should be skipped for precognitive requests, you may need to add a conditional around the side-effect.

</template>
```

We will enhance this implementation by adding live validation powered by Laravel Precognition. To achieve this we will create a precognitive form, passing through the method, url, and initial form data. Then we will:
Copy link

@mreduar mreduar Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
We will enhance this implementation by adding live validation powered by Laravel Precognition. To achieve this we will create a precognitive form, passing through the method, url, and initial form data. Then we will:
We will enhance this implementation by adding live validation powered by Laravel Precognition. To achieve this, we will create a precognitive form, passing through the method, url, and initial form data. Then we will:


- Precognition requests MUST have a `Precognition` header with the value `true`.
- Precognition responses MUST have a `Precognition` header with the value `true`.
- When requesting specific inputs be validated, the `Precognition-Validate-Only` header SHOULD be used. The value MUST be a comma seperated list in input names e.g. `email,phone,address`.
Copy link

@mreduar mreduar Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- When requesting specific inputs be validated, the `Precognition-Validate-Only` header SHOULD be used. The value MUST be a comma seperated list in input names e.g. `email,phone,address`.
- When requesting specific inputs be validated, the `Precognition-Validate-Only` header SHOULD be used. The value MUST be a comma separated list in input names e.g. `email,phone,address`.

});
```

To disable this feature you may specify a fingerprint of `null`. You may disable it globally by passing `null` to `fingerprintRequestsUsing`:
Copy link

@mreduar mreduar Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To disable this feature you may specify a fingerprint of `null`. You may disable it globally by passing `null` to `fingerprintRequestsUsing`:
To disable this feature, you may specify a fingerprint of `null`. You may disable it globally by passing `null` to `fingerprintRequestsUsing`:

<a name="installation"></a>
## Installation

The frontend helper libraries make working with Precognition a dreamy delight. If you are going to use Precognition, we recommend installing the appropriate library for your project. There is a vanilla JavaScript and Vue flavoured package available via NPM:
Copy link

@mreduar mreduar Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The frontend helper libraries make working with Precognition a dreamy delight. If you are going to use Precognition, we recommend installing the appropriate library for your project. There is a vanilla JavaScript and Vue flavoured package available via NPM:
The frontend helper libraries make working with Precognition a dreamy delight. If you are going to use Precognition, we recommend installing the appropriate library for your project. There is a vanilla JavaScript and Vue flavored package available via NPM:

<a name="configuration"></a>
### Configuration

The Precognition configuration object is the [Axios' configuration](https://axios-http.com/docs/req_config) object with some additional options to customize behaviour and also make handling common responses easier.
Copy link

@mreduar mreduar Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The Precognition configuration object is the [Axios' configuration](https://axios-http.com/docs/req_config) object with some additional options to customize behaviour and also make handling common responses easier.
The Precognition configuration object is the [Axios' configuration](https://axios-http.com/docs/req_config) object with some additional options to customize behavior and also make handling common responses easier.

<a name="validating-vue-inertia"></a>
### Working With Vue and Inertia

Inertia has a built-in form helper that makes working with forms a lovely experience. We wanted to maintain this experience while enabling realtime validation with Precognition, so we decided to wrap Inertia's form helper.
Copy link

@vurpa vurpa Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any plans to support React too?

@jiexaspb
Copy link

jiexaspb commented Oct 30, 2022

Hello,

npm install laravel-precognition-vue

this package is not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants