Skip to content

Sharp Context

Sharp provide a way to grab some request context values in the application code.

Ask for context

The class handling the context is Code16\Sharp\Http\Context\CurrentSharpRequest, which is a singleton. So at any point in the request, you can get it via:

php
app(Code16\Sharp\Http\Context\CurrentSharpRequest::class);
app(Code16\Sharp\Http\Context\CurrentSharpRequest::class);

or with the global helper:

php
currentSharpRequest();
currentSharpRequest();

Here's a quick example:

php
class MyForm extends SharpForm
{
    // [...]
    
    function buildFormFields()
    {
        $this->addField(
            SharpFormTextField::make("name")
                ->setReadOnly(currentSharpRequest()->isUpdate())
        );
    }
}
class MyForm extends SharpForm
{
    // [...]
    
    function buildFormFields()
    {
        $this->addField(
            SharpFormTextField::make("name")
                ->setReadOnly(currentSharpRequest()->isUpdate())
        );
    }
}

The context is often useful in a Form situation to display or hide fields depending on the instance status (creation, update), or in a Validator to add an ID exception in a "unique" rule.

Methods

entityKey

Grab the current entity key.

isEntityList

isShow

isForm

Find out the current page type.

isUpdate

isCreation

In Form case, check the current status.

instanceId

In Form and Show Page cases, grab the instance id.

Returns a Collection of Code16\Sharp\Http\Context\Util\BreadcrumbItems

getCurrentBreadcrumbItem

Get the current breadcrumb item.

getPreviousShowFromBreadcrumbItems

Get (if existing) the closest Show in the breadcrumb.

globalFilterFor

Get the value of a Global Filter: see the Global Filter documentation to know more about this feature.

Released under the MIT License.