CodeIgniter v4.6 API

Filters
in package

Filters

Tags
see
FiltersTest

Table of Contents

Properties

$arguments  : array<string, array<int, string>|null>
Any arguments to be passed to filters.
$argumentsClass  : array<class-string, array<int, string>|null>
Any arguments to be passed to filtersClass.
$config  : Filters
The Config\Filters instance
$filterClassInstances  : array<class-string, FilterInterface>
List of filter class instances.
$filters  : array{before: list, after: list}
The filter list to execute for the current request (URI path).
$filtersClass  : array{before: list}>, after: list}>}
The collection of filter classnames and their arguments to execute for the current request (URI path).
$initialized  : bool
Whether we've done initial processing on the filter lists.
$modules  : Modules
The Config\Modules instance
$request  : RequestInterface
The active IncomingRequest or CLIRequest
$response  : ResponseInterface
The active Response instance

Methods

__construct()  : mixed
Constructor.
addFilter()  : $this
Adds a new alias to the config file.
enableFilters()  : Filters
Ensures that specific filters are on and enabled for the current request.
getArguments()  : array<string, string>|string
Returns the arguments for a specified key, or all.
getFilters()  : array{before: list, after: list}
Returns the processed filters array.
getFiltersClass()  : array{before: list}>, after: list}>}
Returns the filtersClass array.
getRequiredClasses()  : array<int, array{0: class-string, 1: list}>
Returns the "Required Filters" class list.
initialize()  : Filters
Runs through our list of filters provided by the configuration object to get them ready for use, including getting uri masks to proper regex, removing those we can from the possibilities based on HTTP method, etc.
reset()  : self
Restores instance to its pre-initialized state.
run()  : RequestInterface|ResponseInterface|string|null
Runs through all the filters (except "Required Filters") for the specified URI and position.
setResponse()  : void
Set the response explicitly.
processAliasesToClass()  : void
Maps filter aliases to the equivalent filter classes
processFilters()  : void
Add any applicable configured filters to the mix.
processGlobals()  : void
Add any applicable (not excluded) global filter settings to the mix.
processMethods()  : void
Add any method-specific filters to the mix.
checkExcept()  : bool
Check except paths
checkPseudoRegex()  : bool
Check the URI path as pseudo-regex
createFilter()  : FilterInterface
discoverFilters()  : void
If discoverFilters is enabled in Config then system will try to auto-discover custom filters files in namespaces and allow access to the config object via the variable $filters as with the routes file.
enableFilter()  : void
Ensures that a specific filter is on and enabled for the current request.
getCleanName()  : array{0: string, 1: list}
Get clean name and arguments
pathApplies()  : bool
Check paths for match for URI
runAfter()  : ResponseInterface
runBefore()  : RequestInterface|ResponseInterface|string
setToolbarToLast()  : array<string|int, mixed>
Set the toolbar filter to the last position to be executed.

Properties

$arguments

Any arguments to be passed to filters.

No longer used.

protected array<string, array<int, string>|null> $arguments = []

[name => params]

$argumentsClass

Any arguments to be passed to filtersClass.

No longer used.

protected array<class-string, array<int, string>|null> $argumentsClass = []

[classname => arguments]

$config

The Config\Filters instance

protected Filters $config

$filterClassInstances

List of filter class instances.

protected array<class-string, FilterInterface> $filterClassInstances = []

[classname => instance]

$filters

The filter list to execute for the current request (URI path).

protected array{before: list, after: list} $filters = ['before' => [], 'after' => []]

This property is for display. Use $filtersClass to execute filters. This does not include "Required Filters".

[ 'before' => [ 'alias', 'alias:arg1', 'alias:arg1,arg2', ], 'after' => [ 'alias', 'alias:arg1', 'alias:arg1,arg2', ], ]

$filtersClass

The collection of filter classnames and their arguments to execute for the current request (URI path).

protected array{before: list}>, after: list}>} $filtersClass = ['before' => [], 'after' => []]

This does not include "Required Filters".

[ 'before' => [ [classname, arguments], ], 'after' => [ [classname, arguments], ], ]

$initialized

Whether we've done initial processing on the filter lists.

protected bool $initialized = false

$modules

The Config\Modules instance

protected Modules $modules

Methods

addFilter()

Adds a new alias to the config file.

public addFilter(string $class[, string|null $alias = null ][, string $position = 'before' ][, string $section = 'globals' ]) : $this

MUST be called prior to initialize(); Intended for use within routes files.

Parameters
$class : string
$alias : string|null = null
$position : string = 'before'
$section : string = 'globals'
Tags
phpstan-param

'before'|'after' $position

Return values
$this

enableFilters()

Ensures that specific filters are on and enabled for the current request.

public enableFilters(array<int, string> $filters[, string $when = 'before' ]) : Filters

Filters can have "arguments". This is done by placing a colon immediately after the filter name, followed by a comma-separated list of arguments that are passed to the filter when executed.

Parameters
$filters : array<int, string>

filter_name or filter_name:arguments like 'role:admin,manager'

$when : string = 'before'
Return values
Filters

getArguments()

Returns the arguments for a specified key, or all.

public getArguments([string|null $key = null ]) : array<string, string>|string

Already does not work.

Parameters
$key : string|null = null
Return values
array<string, string>|string

getFilters()

Returns the processed filters array.

public getFilters() : array{before: list, after: list}

This does not include "Required Filters".

Return values
array{before: list, after: list}

getFiltersClass()

Returns the filtersClass array.

public getFiltersClass() : array{before: list}>, after: list}>}

This does not include "Required Filters".

Return values
array{before: list}>, after: list}>}

getRequiredClasses()

Returns the "Required Filters" class list.

public getRequiredClasses(string $position) : array<int, array{0: class-string, 1: list}>
Parameters
$position : string
Tags
phpstan-param

'before'|'after' $position

Return values
array<int, array{0: class-string, 1: list}>

[[classname, arguments], ...]

initialize()

Runs through our list of filters provided by the configuration object to get them ready for use, including getting uri masks to proper regex, removing those we can from the possibilities based on HTTP method, etc.

public initialize([string|null $uri = null ]) : Filters

The resulting $this->filters is an array of only filters that should be applied to this request.

We go ahead and process the entire tree because we'll need to run through both a before and after and don't want to double process the rows.

Parameters
$uri : string|null = null

URI path relative to baseURL (all lowercase)

Tags
TODO

We don't need to accept null as $uri.

testTag

Only for test code. The run() calls this, so you don't need to call this in your app.

Return values
Filters

reset()

Restores instance to its pre-initialized state.

public reset() : self

Most useful for testing so the service can be re-initialized to a different path.

Return values
self

processAliasesToClass()

Maps filter aliases to the equivalent filter classes

protected processAliasesToClass(string $position) : void
Parameters
$position : string
Tags
phpstan-param

'before'|'after' $position

throws
FilterException

processFilters()

Add any applicable configured filters to the mix.

protected processFilters([string|null $uri = null ]) : void
Parameters
$uri : string|null = null

URI path relative to baseURL (all lowercase)

processGlobals()

Add any applicable (not excluded) global filter settings to the mix.

protected processGlobals([string|null $uri = null ]) : void
Parameters
$uri : string|null = null

URI path relative to baseURL (all lowercase)

processMethods()

Add any method-specific filters to the mix.

protected processMethods() : void

checkExcept()

Check except paths

private checkExcept(string $uri, array<string|int, mixed>|string $paths) : bool
Parameters
$uri : string

URI path relative to baseURL (all lowercase)

$paths : array<string|int, mixed>|string

The except path patterns

Return values
bool

True if the URI matches except paths.

checkPseudoRegex()

Check the URI path as pseudo-regex

private checkPseudoRegex(string $uri, array<string|int, mixed> $paths) : bool
Parameters
$uri : string

URI path relative to baseURL (all lowercase, URL-decoded)

$paths : array<string|int, mixed>

The except path patterns

Return values
bool

discoverFilters()

If discoverFilters is enabled in Config then system will try to auto-discover custom filters files in namespaces and allow access to the config object via the variable $filters as with the routes file.

private discoverFilters() : void

Use Registrar instead.

Sample: $filters->aliases['custom-auth'] = \Acme\Blob\Filters\BlobAuth::class;

enableFilter()

Ensures that a specific filter is on and enabled for the current request.

private enableFilter(string $filter[, string $position = 'before' ]) : void

Filters can have "arguments". This is done by placing a colon immediately after the filter name, followed by a comma-separated list of arguments that are passed to the filter when executed.

Parameters
$filter : string

filter_name or filter_name:arguments like 'role:admin,manager' or filter classname.

$position : string = 'before'
Tags
phpstan-param

'before'|'after' $position

getCleanName()

Get clean name and arguments

private getCleanName(string $filter) : array{0: string, 1: list}
Parameters
$filter : string

filter_name or filter_name:arguments like 'role:admin,manager'

Return values
array{0: string, 1: list}

[name, arguments]

pathApplies()

Check paths for match for URI

private pathApplies(string $uri, array<string|int, mixed>|string $paths) : bool
Parameters
$uri : string

URI to test against

$paths : array<string|int, mixed>|string

The path patterns to test

Return values
bool

True if any of the paths apply to the URI

runAfter()

private runAfter(array<int, array{0: class-string, 1: list}> $filterClassList) : ResponseInterface
Parameters
$filterClassList : array<int, array{0: class-string, 1: list}>

[[classname, arguments], ...]

Return values
ResponseInterface

setToolbarToLast()

Set the toolbar filter to the last position to be executed.

private setToolbarToLast(array<int, string> $filters[, bool $remove = false ]) : array<string|int, mixed>
Parameters
$filters : array<int, string>

after filter array

$remove : bool = false

if true, remove toolbar filter

Return values
array<string|int, mixed>

        
On this page

Search results