Skip to content

Tokens

Type
Class
Namespace
craft\services
Inherits
craft\services\Tokens » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0.0

The Tokens service.

An instance of the service is available via Craft::$app->tokens.

View source

Public Properties

PropertyDescription
behaviorsyii\base\Behavior – List of behaviors attached to this component.

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of a component property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets the value of a component property.
__unset()Sets a component property to be null.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
behaviors()Returns a list of behaviors that this component should behave as.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
createPreviewToken()Creates a new token for previewing content, using the config4:previewTokenDuration to determine the duration, if set.
createToken()Creates a new token and returns it.
deleteExpiredTokens()Deletes any expired tokens.
deleteTokenById()Deletes a token by its ID.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getTokenRoute()Searches for a token, and possibly returns a route for the request.
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined for this component.
incrementTokenUsageCountById()Increments a token's usage count.
init()Initializes the object.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
trigger()Triggers an event.

createPreviewToken()

Since
3.7.0

Creates a new token for previewing content, using the config4:previewTokenDuration to determine the duration, if set.

View source

Arguments

  • $route (mixed) – Where matching requests should be routed to.
  • $usageLimit (integer, null) – The maximum number of times this token can be used. Defaults to no limit.
  • $token (string, null) – The token to use, if it was pre-generated. Must be exactly 32 characters.

Returns

string, false – The generated token, or false if there was an error.

createToken()

Creates a new token and returns it.

View source

Arguments

  • $route (array, string) – Where matching requests should be routed to.
  • $usageLimit (integer, null) – The maximum number of times this token can be used. Defaults to no limit.
  • $expiryDate (DateTime, null) – The date that the token expires. Defaults to the 'defaultTokenDuration' config setting.
  • $token (string, null) – The token to use, if it was pre-generated. Must be exactly 32 characters.

Returns

string, false – The generated token, or false if there was an error.


Example

php
// Route to a controller action
Craft::$app->tokens->createToken('action/path');

// Route to a controller action with params
Craft::$app->tokens->createToken(['action/path', [
    'foo' => 'bar'
]]);

// Route to a template
Craft::$app->tokens->createToken([
    'templates/render',
    [
        'template' => 'template/path',
    ]
]);

deleteExpiredTokens()

Deletes any expired tokens.

View source

Returns

boolean

deleteTokenById()

Deletes a token by its ID.

View source

Arguments

Returns

boolean

getTokenRoute()

Searches for a token, and possibly returns a route for the request.

View source

Arguments

Returns

array, false

incrementTokenUsageCountById()

Increments a token's usage count.

View source

Arguments

Returns

boolean