AssetsController ​
- Type
- Class
- Namespace
- craft\controllers
- Inherits
- craft\controllers\AssetsController » craft\web\Controller » yii\web\Controller » yii\base\Controller » yii\base\Component » yii\base\BaseObject
- Implements
- yii\base\Configurable, yii\base\ViewContextInterface
- Since
- 3.0.0
The AssetsController class is a controller that handles various actions related to asset tasks, such as uploading files and creating/deleting/renaming files and folders.
Note that all actions in the controller except for actionGenerateTransform() and actionGenerateThumb() require an authenticated Craft session via allowAnonymous.
Public Properties ​
Property | Description |
---|---|
action | yii\base\Action, null – The action that is currently being executed. |
actionParams | array – The parameters bound to the current action. |
behaviors | yii\base\Behavior – List of behaviors attached to this component. |
defaultAction | string – The ID of the action that is used when the action ID is not specified in the request. |
enableCsrfValidation | boolean – Whether to enable CSRF validation for the actions in this controller. |
id | string – The ID of this controller. |
layout | string, null, false – The name of the layout to be applied to this controller's views. |
module | yii\base\Module – The module that this controller belongs to. |
modules | yii\base\Module – All ancestor modules that this controller is located within. |
request | craft\web\Request |
response | craft\web\Response |
route | string – The route (module ID, controller ID and action ID) of the current request. |
uniqueId | string – The controller ID that is prefixed with the module ID (if any). |
view | craft\web\View – The view object that can be used to render views or view files |
viewPath | string – The directory containing the view files for this controller. |
Protected Properties ​
Property | Description |
---|---|
allowAnonymous | integer, boolean, integer[], string[] – Whether this controller’s actions can be accessed anonymously. |
allowAnonymous
​
Whether this controller’s actions can be accessed anonymously.
This can be set to any of the following:
false
orself::ALLOW_ANONYMOUS_NEVER
(default) – indicates that all controller actions should never be accessed anonymouslytrue
orself::ALLOW_ANONYMOUS_LIVE
– indicates that all controller actions can be accessed anonymously when the system is liveself::ALLOW_ANONYMOUS_OFFLINE
– indicates that all controller actions can be accessed anonymously when the system is offlineself::ALLOW_ANONYMOUS_LIVE | self::ALLOW_ANONYMOUS_OFFLINE
– indicates that all controller actions can be accessed anonymously when the system is live or offline- An array of action IDs (e.g.
['save-guest-entry', 'edit-guest-entry']
) – indicates that the listed action IDs can be accessed anonymously when the system is live - An array of action ID/bitwise pairs (e.g.
['save-guest-entry' => self::ALLOW_ANONYMOUS_OFFLINE]
– indicates that the listed action IDs can be accessed anonymously per the bitwise int assigned to it.
Public Methods ​
Method | Description |
---|---|
__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() | |
__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. |
actionCreateFolder() | Creates a folder. |
actionDeleteAsset() | Deletes an asset. |
actionDeleteFolder() | Delete a folder. |
actionDownloadAsset() | Returns a file’s contents. |
actionEditAsset() | Edits an asset. |
actionEditImage() | Returns the image being edited. |
actionGenerateThumb() | Generates a thumbnail. |
actionGenerateTransform() | Generates a transform. |
actionImageEditor() | Returns the Image Editor template. |
actionIndex() | Displays the Assets index page. |
actionMoveAsset() | Move one or more assets. |
actionMoveFolder() | Moves a folder. |
actionPreviewFile() | Returns file preview info for an asset. |
actionPreviewThumb() | Returns an updated preview image for an asset. |
actionRenameFolder() | Renames a folder. |
actionReplaceFile() | Replaces a file. |
actionSaveAsset() | Saves an asset. |
actionSaveImage() | Saves an image according to the posted parameters. |
actionThumb() | Returns a thumbnail’s contents. |
actionUpload() | Handles a file upload. |
actions() | Declares external actions for the controller. |
afterAction() | This method is invoked right after an action is executed. |
asErrorJson() | Responds to the request with a JSON error message. |
asJson() | Send data formatted as JSON. |
asJsonP() | Sets the response format of the given data as JSONP. |
asRaw() | Sets the response format of the given data as RAW. |
asXml() | Send data formatted as XML. |
attachBehavior() | Attaches a behavior to this component. |
attachBehaviors() | Attaches a list of behaviors to the component. |
beforeAction() | This method is invoked right before an action is executed. |
behaviors() | Returns a list of behaviors that this component should behave as. |
bindActionParams() | Binds the parameters to the action. |
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. |
createAction() | Creates an action based on the given action 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. |
findLayoutFile() | Finds the applicable layout file. |
getBehavior() | Returns the named behavior object. |
getBehaviors() | Returns all behaviors attached to this component. |
getModules() | Returns all ancestor modules of this controller. |
getRoute() | Returns the route of the current request. |
getUniqueId() | Returns the unique ID of the controller. |
getView() | Returns the view object that can be used to render views or view files |
getViewPath() | Returns the directory containing view files for this controller. |
goBack() | Redirects the browser to the last visited page. |
goHome() | Redirects the browser to the home page. |
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. |
init() | Initializes the object. |
off() | Detaches an existing event handler from this component. |
on() | Attaches an event handler to an event. |
redirect() | Redirects the browser to the specified URL. |
redirectToPostedUrl() | Redirects to the URI specified in the POST. |
refresh() | Refreshes the current page. |
render() | Renders a view and applies layout if available. |
renderAjax() | Renders a view in response to an AJAX request. |
renderContent() | Renders a static string by applying a layout. |
renderFile() | Renders a view file. |
renderPartial() | Renders a view without applying layout. |
renderTemplate() | Renders a template. |
requireAcceptsJson() | Throws a 400 error if the request doesn't accept JSON. |
requireAdmin() | Throws a 403 error if the current user is not an admin. |
requireAuthorization() | Checks whether the current user can perform a given action, and ends the request with a 403 error if they don’t. |
requireCpRequest() | Throws a 400 error if the current request isn’t a control panel request. |
requireElevatedSession() | Requires that the user has an elevated session. |
requireGuest() | Redirects the user to the account template if they are logged in. |
requireLogin() | Redirects the user to the login template if they're not logged in. |
requirePermission() | Checks whether the current user has a given permission, and ends the request with a 403 error if they don’t. |
requirePostRequest() | Throws a 400 error if this isn’t a POST request |
requireSiteRequest() | Throws a 400 error if the current request isn’t a site request. |
requireToken() | Throws a 400 error if the current request doesn’t have a valid Craft token. |
run() | Runs a request specified in terms of a route. |
runAction() | Runs an action within this controller with the specified action ID and parameters. |
setFailFlash() | Sets an error flash message on the user session. |
setSuccessFlash() | Sets a success flash message on the user session. |
setView() | Sets the view object to be used by this controller. |
setViewPath() | Sets the directory that contains the view files. |
trigger() | Triggers an event. |
actionCreateFolder()
​
Creates a folder.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if the parent folder cannot be found
actionDeleteAsset()
​
Deletes an asset.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if the folder cannot be found - yii\web\ForbiddenHttpException
- yii\base\UserException
actionDeleteFolder()
​
Delete a folder.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if the folder cannot be found
actionDownloadAsset()
​
Returns a file’s contents.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if the file to download cannot be found.
actionEditAsset()
​
- Since
- 3.4.0
Edits an asset.
Arguments ​
$assetId
(integer) – The asset ID$asset
(craft\elements\Asset, null) – The asset being edited, if there were any validation errors.$site
(string, null) – The site handle, if specified.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if$assetId
is invalid - yii\web\ForbiddenHttpException
if the user isn't permitted to edit the asset
actionEditImage()
​
Returns the image being edited.
Returns ​
Throws ​
actionGenerateThumb()
​
DEPRECATED
Deprecated in 3.0.13. Use actionThumb() instead.
Generates a thumbnail.
Arguments ​
$uid
(string) – The asset's UID$width
(integer) – The thumbnail width$height
(integer) – The thumbnail height
Returns ​
actionGenerateTransform()
​
Generates a transform.
Arguments ​
Returns ​
Throws ​
- yii\web\NotFoundHttpException
if the transform can't be found - yii\web\ServerErrorHttpException
if the transform can't be generated
actionImageEditor()
​
Returns the Image Editor template.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if the asset is missing.
actionIndex()
​
- Since
- 3.8.0
Displays the Assets index page.
Arguments ​
$defaultSource
Returns ​
actionMoveAsset()
​
Move one or more assets.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if the asset or the target folder cannot be found
actionMoveFolder()
​
Moves a folder.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if the folder to move, or the destination parent folder, cannot be found
actionPreviewFile()
​
Returns file preview info for an asset.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if not a valid request
actionPreviewThumb()
​
- Since
- 3.4.0
Returns an updated preview image for an asset.
Returns ​
Throws ​
actionRenameFolder()
​
Renames a folder.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if the folder cannot be found
actionReplaceFile()
​
Replaces a file.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if incorrect combination of parameters passed. - yii\web\NotFoundHttpException
if the asset can’t be found
actionSaveAsset()
​
- Since
- 3.4.0
Saves an asset.
Returns ​
actionSaveImage()
​
Saves an image according to the posted parameters.
Returns ​
Throws ​
- yii\web\BadRequestHttpException
if some parameters are missing. - Throwable
if something went wrong saving the asset.
actionThumb()
​
- Since
- 3.0.13
Returns a thumbnail’s contents.
Arguments ​
$uid
(string) – The asset's UID$width
(integer) – The thumbnail width$height
(integer) – The thumbnail height
Returns ​
actionUpload()
​
- Since
- 3.4.0
Handles a file upload.
Returns ​
Throws ​
Protected Methods ​
Method | Description |
---|---|
asBrokenImage() | Sends a broken image response based on a given exception. |
bindInjectedParams() | Fills parameters based on types and names in action method signature. |
requirePeerVolumePermissionByAsset() | Requires a peer permission for a given asset, unless it was uploaded by the current user. |
requireVolumePermission() | Requires a volume permission by its UID. |
requireVolumePermissionByAsset() | Requires a volume permission for a given asset. |
requireVolumePermissionByFolder() | Requires a volume permission for a given folder. |
asBrokenImage()
​
- Since
- 3.4.8
Sends a broken image response based on a given exception.
Arguments ​
Returns ​
requirePeerVolumePermissionByAsset()
​
- Since
- 3.4.8
Requires a peer permission for a given asset, unless it was uploaded by the current user.
Arguments ​
$permissionName
(string) – The name of the peer permission to require (sans:<volume-uid>
suffix)$asset
(craft\elements\Asset) – The asset whose volume should be checked
Throws ​
requireVolumePermission()
​
- Since
- 3.4.8
Requires a volume permission by its UID.
Arguments ​
$permissionName
(string) – The name of the peer permission to require (sans:<volume-uid>
suffix)$volumeUid
(string) – The volume’s UID
Throws ​
requireVolumePermissionByAsset()
​
- Since
- 3.4.8
Requires a volume permission for a given asset.
Arguments ​
$permissionName
(string) – The name of the permission to require (sans:<volume-uid>
suffix)$asset
(craft\elements\Asset) – The asset whose volume should be checked
Throws ​
requireVolumePermissionByFolder()
​
- Since
- 3.4.8
Requires a volume permission for a given folder.
Arguments ​
$permissionName
(string) – The name of the peer permission to require (sans:<volume-uid>
suffix)$folder
(craft\models\VolumeFolder) – The folder whose volume should be checked
Throws ​
Constants ​
Constant | Description |
---|---|
ALLOW_ANONYMOUS_LIVE | |
ALLOW_ANONYMOUS_NEVER | |
ALLOW_ANONYMOUS_OFFLINE |