Neuron-PHP

Forbidden extends Exception
in package

Exception thrown when an authenticated user lacks permission to access a resource.

This exception should be thrown when a user is authenticated but does not have the necessary permissions to access a resource. It corresponds to HTTP 403 Forbidden status code.

Tags
example
// Throw when user lacks specific permission
if (!$user->hasPermission('admin.edit')) {
    throw new Forbidden('You do not have permission to edit', 'User', 'admin.edit');
}

// Throw when accessing forbidden resource
if (!$this->canAccessResource($user, $document)) {
    throw new Forbidden('Access denied to document', 'Document #' . $document->id);
}

Table of Contents

Properties

$permission  : string|null
$resource  : string|null

Methods

__construct()  : mixed
getPermission()  : string|null
Get the permission that was lacking
getResource()  : string|null
Get the resource that was forbidden

Properties

Methods

__construct()

public __construct([string $message = 'Access forbidden' ][, string|null $resource = null ][, string|null $permission = null ][, int $code = 403 ][, Throwable|null $previous = null ]) : mixed
Parameters
$message : string = 'Access forbidden'

The error message

$resource : string|null = null

The resource that was forbidden (e.g., 'User Profile', 'Document #123')

$permission : string|null = null

The permission that was lacking (e.g., 'admin.edit', 'document.read')

$code : int = 403

The exception code (defaults to 403)

$previous : Throwable|null = null

Previous exception for chaining

getPermission()

Get the permission that was lacking

public getPermission() : string|null
Return values
string|null

getResource()

Get the resource that was forbidden

public getResource() : string|null
Return values
string|null

        
On this page

Search results