Class yii\base\Controller

Inheritanceyii\base\Controller » yii\base\Component » yii\base\Object
Implementsyii\base\Configurable, yii\base\ViewContextInterface
Subclassesapp\controllers\SiteController, backend\controllers\FilesController, backend\controllers\RolesCheckerController, backend\controllers\SettingsController, backend\controllers\SiteController, frontend\controllers\FilesController, frontend\controllers\SiteController, lispa\amos\admin\commands\UserUtilityController, lispa\amos\admin\controllers\DefaultController, lispa\amos\admin\controllers\FirstAccessWizardController, lispa\amos\admin\controllers\SecurityController, lispa\amos\admin\controllers\UserContactController, lispa\amos\admin\controllers\UserDropController, lispa\amos\admin\controllers\UserProfileAjaxController, lispa\amos\admin\controllers\UserProfileController, lispa\amos\admin\controllers\base\UserProfileController, lispa\amos\attachments\controllers\FileController, lispa\amos\chat\console\ChatController, lispa\amos\chat\controllers\DefaultController, lispa\amos\comments\controllers\CommentController, lispa\amos\comments\controllers\CommentReplyController, lispa\amos\community\controllers\CommunityController, lispa\amos\community\controllers\CommunityWizardController, lispa\amos\community\controllers\DefaultController, lispa\amos\community\controllers\JoinController, lispa\amos\community\controllers\ReportsController, lispa\amos\community\controllers\SubcommunitiesController, lispa\amos\community\controllers\api\CommunityController, lispa\amos\community\controllers\base\CommunityController, lispa\amos\comuni\controllers\DashboardController, lispa\amos\comuni\controllers\DefaultController, lispa\amos\comuni\controllers\ImportatoreController, lispa\amos\comuni\controllers\IstatCittaMetropolitaneController, lispa\amos\comuni\controllers\IstatComuniController, lispa\amos\comuni\controllers\IstatContinentiController, lispa\amos\comuni\controllers\IstatNazioniController, lispa\amos\comuni\controllers\IstatProvinceController, lispa\amos\comuni\controllers\IstatRegioniController, lispa\amos\comuni\controllers\IstatUnioneDeiComuniController, lispa\amos\comuni\controllers\api\IstatCittaMetropolitaneController, lispa\amos\comuni\controllers\api\IstatComuniController, lispa\amos\comuni\controllers\api\IstatContinentiController, lispa\amos\comuni\controllers\api\IstatNazioniController, lispa\amos\comuni\controllers\api\IstatProvinceController, lispa\amos\comuni\controllers\api\IstatRegioniController, lispa\amos\comuni\controllers\api\IstatUnioneDeiComuniController, lispa\amos\comuni\controllers\base\AjaxController, lispa\amos\comuni\controllers\base\IstatCittaMetropolitaneController, lispa\amos\comuni\controllers\base\IstatComuniController, lispa\amos\comuni\controllers\base\IstatContinentiController, lispa\amos\comuni\controllers\base\IstatNazioniController, lispa\amos\comuni\controllers\base\IstatProvinceController, lispa\amos\comuni\controllers\base\IstatRegioniController, lispa\amos\comuni\controllers\base\IstatUnioneDeiComuniController, lispa\amos\core\commands\LanguageSourceController, lispa\amos\core\controllers\AmosController, lispa\amos\core\controllers\BackendController, lispa\amos\core\controllers\BaseController, lispa\amos\core\controllers\CrudController, lispa\amos\core\controllers\FrontendController, lispa\amos\core\giiamos\controllers\AjaxController, lispa\amos\core\giiamos\controllers\DefaultController, lispa\amos\core\migration\MigrateController, lispa\amos\cwh\controllers\ConfigurationController, lispa\amos\cwh\controllers\CwhAjaxController, lispa\amos\cwh\controllers\CwhAuthAssignmentController, lispa\amos\cwh\controllers\CwhConfigController, lispa\amos\cwh\controllers\CwhNodiController, lispa\amos\cwh\controllers\CwhPubblicazioniController, lispa\amos\cwh\controllers\CwhRegolePubblicazioneController, lispa\amos\dashboard\commands\DashboardController, lispa\amos\dashboard\controllers\DefaultController, lispa\amos\dashboard\controllers\ManagerController, lispa\amos\dashboard\controllers\SubDashboardController, lispa\amos\dashboard\controllers\base\DashboardController, lispa\amos\discussioni\controllers\DefaultController, lispa\amos\discussioni\controllers\DiscussioniCommentiController, lispa\amos\discussioni\controllers\DiscussioniRisposteController, lispa\amos\discussioni\controllers\DiscussioniTopicController, lispa\amos\discussioni\controllers\DiscussioniTopicWizardController, lispa\amos\documenti\controllers\DefaultController, lispa\amos\documenti\controllers\DocumentiCategorieController, lispa\amos\documenti\controllers\DocumentiController, lispa\amos\documenti\controllers\DocumentiWizardController, lispa\amos\documenti\controllers\HierarchicalDocumentsController, lispa\amos\een\commands\controllers\ImportController, lispa\amos\een\controllers\ApiController, lispa\amos\een\controllers\EenExprOfInterestController, lispa\amos\een\controllers\EenPartnershipProposalController, lispa\amos\emailmanager\commands\EmailSpoolController, lispa\amos\emailmanager\controllers\DefaultController, lispa\amos\emailmanager\controllers\EmailSpoolController, lispa\amos\emailmanager\controllers\EmailTemplateController, lispa\amos\events\controllers\DefaultController, lispa\amos\events\controllers\EventController, lispa\amos\events\controllers\EventTypeController, lispa\amos\events\controllers\EventWizardController, lispa\amos\events\controllers\base\EventController, lispa\amos\events\controllers\base\EventTypeController, lispa\amos\faq\controllers\DefaultController, lispa\amos\faq\controllers\FaqCategoriesController, lispa\amos\faq\controllers\FaqController, lispa\amos\faq\controllers\FaqStatoController, lispa\amos\favorites\controllers\FavoriteController, lispa\amos\inforeq\controllers\InforeqArgomentiController, lispa\amos\inforeq\controllers\InforeqController, lispa\amos\inforeq\controllers\api\InforeqArgomentiController, lispa\amos\inforeq\controllers\api\InforeqController, lispa\amos\inforeq\controllers\base\InforeqArgomentiController, lispa\amos\inforeq\controllers\base\InforeqController, lispa\amos\invitations\controllers\InvitationController, lispa\amos\invitations\controllers\api\InvitationController, lispa\amos\invitations\controllers\base\InvitationController, lispa\amos\layout\controllers\MaintenanceController, lispa\amos\mobile\bridge\controllers\NotificationController, lispa\amos\mobile\bridge\modules\v1\controllers\ChatController, lispa\amos\mobile\bridge\modules\v1\controllers\CommentsController, lispa\amos\mobile\bridge\modules\v1\controllers\DefaultController, lispa\amos\mobile\bridge\modules\v1\controllers\EntityDataController, lispa\amos\mobile\bridge\modules\v1\controllers\ModuleController, lispa\amos\mobile\bridge\modules\v1\controllers\SecurityController, lispa\amos\mobile\bridge\modules\v1\controllers\UserController, lispa\amos\myactivities\controllers\MyActivitiesController, lispa\amos\news\controllers\DefaultController, lispa\amos\news\controllers\NewsCategorieController, lispa\amos\news\controllers\NewsController, lispa\amos\news\controllers\NewsWizardController, lispa\amos\notificationmanager\commands\NotifierController, lispa\amos\organizzazioni\controllers\DefaultController, lispa\amos\organizzazioni\controllers\ProfiloController, lispa\amos\organizzazioni\controllers\api\ProfiloController, lispa\amos\organizzazioni\controllers\base\ProfiloController, lispa\amos\partnershipprofiles\console\PartnershipProfilesController, lispa\amos\partnershipprofiles\controllers\DefaultController, lispa\amos\partnershipprofiles\controllers\ExpressionsOfInterestController, lispa\amos\partnershipprofiles\controllers\PartnershipProfilesController, lispa\amos\partnershipprofiles\controllers\base\ExpressionsOfInterestController, lispa\amos\partnershipprofiles\controllers\base\PartnershipProfilesController, lispa\amos\privileges\controllers\DefaultController, lispa\amos\privileges\controllers\PrivilegesController, lispa\amos\projectmanagement\controllers\CreateProjectController, lispa\amos\projectmanagement\controllers\DefaultController, lispa\amos\projectmanagement\controllers\NewProjectWizardController, lispa\amos\projectmanagement\controllers\ProjectsActivitiesController, lispa\amos\projectmanagement\controllers\ProjectsController, lispa\amos\projectmanagement\controllers\ProjectsCustomCostItemsController, lispa\amos\projectmanagement\controllers\ProjectsGanttController, lispa\amos\projectmanagement\controllers\ProjectsMeasureController, lispa\amos\projectmanagement\controllers\ProjectsModifyRequestController, lispa\amos\projectmanagement\controllers\ProjectsProgressCostsController, lispa\amos\projectmanagement\controllers\ProjectsProgressDetectionPeriodConsoleController, lispa\amos\projectmanagement\controllers\ProjectsProgressDetectionPeriodController, lispa\amos\projectmanagement\controllers\ProjectsProgressStatementsController, lispa\amos\projectmanagement\controllers\ProjectsStatesController, lispa\amos\projectmanagement\controllers\ProjectsTaskStatusesController, lispa\amos\projectmanagement\controllers\ProjectsTasksController, lispa\amos\projectmanagement\controllers\RestLinkController, lispa\amos\projectmanagement\controllers\api\ProjectsActivitiesController, lispa\amos\projectmanagement\controllers\api\ProjectsController, lispa\amos\projectmanagement\controllers\api\ProjectsMeasureController, lispa\amos\projectmanagement\controllers\api\ProjectsStatesController, lispa\amos\projectmanagement\controllers\api\ProjectsTaskStatusesController, lispa\amos\projectmanagement\controllers\api\ProjectsTasksController, lispa\amos\projectmanagement\controllers\base\ProjectsActivitiesController, lispa\amos\projectmanagement\controllers\base\ProjectsController, lispa\amos\projectmanagement\controllers\base\ProjectsCustomCostItemsController, lispa\amos\projectmanagement\controllers\base\ProjectsMeasureController, lispa\amos\projectmanagement\controllers\base\ProjectsModifyRequestController, lispa\amos\projectmanagement\controllers\base\ProjectsProgressCostsController, lispa\amos\projectmanagement\controllers\base\ProjectsProgressDetectionPeriodController, lispa\amos\projectmanagement\controllers\base\ProjectsProgressStatementsController, lispa\amos\projectmanagement\controllers\base\ProjectsStatesController, lispa\amos\projectmanagement\controllers\base\ProjectsTaskStatusesController, lispa\amos\projectmanagement\controllers\base\ProjectsTasksController, lispa\amos\report\controllers\DefaultController, lispa\amos\report\controllers\ReportController, lispa\amos\risultati\controllers\BisognoPerSviluppoRisultatiController, lispa\amos\risultati\controllers\DefaultController, lispa\amos\risultati\controllers\ProtezioneInnovazioneRisultatiController, lispa\amos\risultati\controllers\RisultatiController, lispa\amos\risultati\controllers\StadioSviluppoRisultatiController, lispa\amos\risultati\controllers\TecnologieRisultatiController, lispa\amos\risultati\controllers\api\BisognoPerSviluppoRisultatiController, lispa\amos\risultati\controllers\api\ProtezioneInnovazioneRisultaController, lispa\amos\risultati\controllers\api\RisultatiController, lispa\amos\risultati\controllers\api\StadioSviluppoRisultatiController, lispa\amos\risultati\controllers\api\TecnologieRisultatiController, lispa\amos\risultati\controllers\base\BisognoPerSviluppoRisultatiController, lispa\amos\risultati\controllers\base\ProtezioneInnovazioneRisultatiController, lispa\amos\risultati\controllers\base\RisultatiController, lispa\amos\risultati\controllers\base\StadioSviluppoRisultatiController, lispa\amos\risultati\controllers\base\TecnologieRisultatiController, lispa\amos\seo\controllers\ApiController, lispa\amos\seo\controllers\ToolsController, lispa\amos\slideshow\controllers\DefaultController, lispa\amos\slideshow\controllers\SlideshowController, lispa\amos\slideshow\controllers\SlideshowPageController, lispa\amos\slideshow\controllers\SlideshowRouteController, lispa\amos\slideshow\controllers\api\SlideshowController, lispa\amos\slideshow\controllers\api\SlideshowPageController, lispa\amos\slideshow\controllers\api\SlideshowRouteController, lispa\amos\slideshow\controllers\base\SlideshowController, lispa\amos\slideshow\controllers\base\SlideshowPageController, lispa\amos\slideshow\controllers\base\SlideshowRouteController, lispa\amos\socialauth\controllers\SocialAuthController, lispa\amos\sondaggi\controllers\AjaxController, lispa\amos\sondaggi\controllers\PubblicazioneController, lispa\amos\sondaggi\controllers\SondaggiController, lispa\amos\sondaggi\controllers\SondaggiDomandeController, lispa\amos\sondaggi\controllers\SondaggiDomandePagineController, lispa\amos\sondaggi\controllers\SondaggiDomandeTipologieController, lispa\amos\sondaggi\controllers\SondaggiRisposteController, lispa\amos\sondaggi\controllers\SondaggiRispostePredefiniteController, lispa\amos\sondaggi\controllers\SondaggiRisposteSessioniController, lispa\amos\tag\controllers\DefaultController, lispa\amos\tag\controllers\ManagerController, lispa\amos\translation\controllers\DefaultController, lispa\amos\translation\controllers\TranslateController, lispa\amos\translation\controllers\TranslatemanagerController, lispa\amos\videoconference\controllers\CronController, lispa\amos\videoconference\controllers\DefaultController, lispa\amos\videoconference\controllers\VideoconfController, pendalf89\filemanager\controllers\DefaultController, pendalf89\filemanager\controllers\FileController, yii\console\Controller, yii\console\controllers\AssetController, yii\console\controllers\BaseMigrateController, yii\console\controllers\CacheController, yii\console\controllers\FixtureController, yii\console\controllers\HelpController, yii\console\controllers\MessageController, yii\console\controllers\MigrateController, yii\console\controllers\ServeController, yii\debug\controllers\DefaultController, yii\debug\controllers\UserController, yii\faker\FixtureController, yii\gii\console\GenerateController, yii\gii\controllers\DefaultController, yii\rest\ActiveController, yii\rest\Controller, yii\web\Controller
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/base/Controller.php

Controller is the base class for classes containing controller logic.

For more details and usage information on Controller, see the guide article on controllers.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$action yii\base\Action The action that is currently being executed. yii\base\Controller
$behaviors yii\base\Behavior[] List of behaviors attached to this component yii\base\Component
$defaultAction string The ID of the action that is used when the action ID is not specified in the request. yii\base\Controller
$id string The ID of this controller. yii\base\Controller
$layout null|string|false The name of the layout to be applied to this controller's views. yii\base\Controller
$module yii\base\Module The module that this controller belongs to. yii\base\Controller
$modules yii\base\Module[] All ancestor modules that this controller is located within. yii\base\Controller
$route string The route (module ID, controller ID and action ID) of the current request. yii\base\Controller
$uniqueId string The controller ID that is prefixed with the module ID (if any). yii\base\Controller
$view yii\base\View|yii\web\View The view object that can be used to render views or view files. yii\base\Controller
$viewPath string The directory containing the view files for this controller. yii\base\Controller

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() yii\base\Controller
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
actions() Declares external actions for the controller. yii\base\Controller
afterAction() This method is invoked right after an action is executed. yii\base\Controller
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
beforeAction() This method is invoked right before an action is executed. yii\base\Controller
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
bindActionParams() Binds the parameters to the action. yii\base\Controller
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\Object
createAction() Creates an action based on the given action ID. yii\base\Controller
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
findLayoutFile() Finds the applicable layout file. yii\base\Controller
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getModules() Returns all ancestor modules of this controller. yii\base\Controller
getRoute() Returns the route of the current request. yii\base\Controller
getUniqueId() Returns the unique ID of the controller. yii\base\Controller
getView() Returns the view object that can be used to render views or view files. yii\base\Controller
getViewPath() Returns the directory containing view files for this controller. yii\base\Controller
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the object. yii\base\Object
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
render() Renders a view and applies layout if available. yii\base\Controller
renderContent() Renders a static string by applying a layout. yii\base\Controller
renderFile() Renders a view file. yii\base\Controller
renderPartial() Renders a view without applying layout. yii\base\Controller
run() Runs a request specified in terms of a route. yii\base\Controller
runAction() Runs an action within this controller with the specified action ID and parameters. yii\base\Controller
setView() Sets the view object to be used by this controller. yii\base\Controller
setViewPath() Sets the directory that contains the view files. yii\base\Controller
trigger() Triggers an event. yii\base\Component

Events

Hide inherited events

EventTypeDescriptionDefined By
EVENT_AFTER_ACTION yii\base\ActionEvent An event raised right after executing a controller action. yii\base\Controller
EVENT_BEFORE_ACTION yii\base\ActionEvent An event raised right before executing a controller action. yii\base\Controller

Property Details

$action public property

The action that is currently being executed. This property will be set by run() when it is called by yii\base\Application to run an action.

public yii\base\Action $action null
$defaultAction public property

The ID of the action that is used when the action ID is not specified in the request. Defaults to 'index'.

public string $defaultAction 'index'
$id public property

The ID of this controller.

public string $id null
$layout public property

The name of the layout to be applied to this controller's views. This property mainly affects the behavior of render(). Defaults to null, meaning the actual layout value should inherit that from $module's layout value. If false, no layout will be applied.

public null|string|false $layout null
$module public property

The module that this controller belongs to.

public yii\base\Module $module null
$modules public read-only property

All ancestor modules that this controller is located within.

$route public read-only property

The route (module ID, controller ID and action ID) of the current request.

public string getRoute ( )
$uniqueId public read-only property

The controller ID that is prefixed with the module ID (if any).

public string getUniqueId ( )
$view public property

The view object that can be used to render views or view files.

public yii\base\View|yii\web\View getView ( )
public void setView ( $view )
$viewPath public property

The directory containing the view files for this controller.

public string getViewPath ( )
public void setViewPath ( $path )

Method Details

__construct() public method

public void __construct ( $id, $module, $config = [] )
$id string

The ID of this controller.

$module yii\base\Module

The module that this controller belongs to.

$config array

Name-value pairs that will be used to initialize the object properties.

actions() public method

Declares external actions for the controller.

This method is meant to be overwritten to declare external actions for the controller. It should return an array, with array keys being action IDs, and array values the corresponding action class names or action configuration arrays. For example,

return [
    'action1' => 'app\components\Action1',
    'action2' => [
        'class' => 'app\components\Action2',
        'property1' => 'value1',
        'property2' => 'value2',
    ],
];

Yii::createObject() will be used later to create the requested action using the configuration provided here.

public void actions ( )
afterAction() public method

This method is invoked right after an action is executed.

The method will trigger the EVENT_AFTER_ACTION event. The return value of the method will be used as the action return value.

If you override this method, your code should look like the following:

public function afterAction($action, $result)
{
    $result = parent::afterAction($action, $result);
    // your custom code here
    return $result;
}
public mixed afterAction ( $action, $result )
$action yii\base\Action

The action just executed.

$result mixed

The action return result.

return mixed

The processed action result.

beforeAction() public method

This method is invoked right before an action is executed.

The method will trigger the EVENT_BEFORE_ACTION event. The return value of the method will determine whether the action should continue to run.

In case the action should not run, the request should be handled inside of the beforeAction code by either providing the necessary output or redirecting the request. Otherwise the response will be empty.

If you override this method, your code should look like the following:

public function beforeAction($action)
{
    // your custom code here, if you want the code to run before action filters,
    // which are triggered on the [[EVENT_BEFORE_ACTION]] event, e.g. PageCache or AccessControl

    if (!parent::beforeAction($action)) {
        return false;
    }

    // other custom code here

    return true; // or false to not run the action
}
public boolean beforeAction ( $action )
$action yii\base\Action

The action to be executed.

return boolean

Whether the action should continue to run.

bindActionParams() public method

Binds the parameters to the action.

This method is invoked by yii\base\Action when it begins to run with the given parameters.

public array bindActionParams ( $action, $params )
$action yii\base\Action

The action to be bound with parameters.

$params array

The parameters to be bound to the action.

return array

The valid parameters that the action can run with.

createAction() public method

Creates an action based on the given action ID.

The method first checks if the action ID has been declared in actions(). If so, it will use the configuration declared there to create the action object. If not, it will look for a controller method whose name is in the format of actionXyz where Xyz stands for the action ID. If found, an yii\base\InlineAction representing that method will be created and returned.

public yii\base\Action|null createAction ( $id )
$id string

The action ID.

return yii\base\Action|null

The newly created action instance. Null if the ID doesn't resolve into any action.

findLayoutFile() public method

Finds the applicable layout file.

public string|boolean findLayoutFile ( $view )
$view yii\base\View

The view object to render the layout file.

return string|boolean

The layout file path, or false if layout is not needed. Please refer to render() on how to specify this parameter.

throws yii\base\InvalidParamException

if an invalid path alias is used to specify the layout.

getModules() public method

Returns all ancestor modules of this controller.

The first module in the array is the outermost one (i.e., the application instance), while the last is the innermost one.

public yii\base\Module[] getModules ( )
return yii\base\Module[]

All ancestor modules that this controller is located within.

getRoute() public method

Returns the route of the current request.

public string getRoute ( )
return string

The route (module ID, controller ID and action ID) of the current request.

getUniqueId() public method

Returns the unique ID of the controller.

public string getUniqueId ( )
return string

The controller ID that is prefixed with the module ID (if any).

getView() public method

Returns the view object that can be used to render views or view files.

The render(), renderPartial() and renderFile() methods will use this view object to implement the actual view rendering. If not set, it will default to the "view" application component.

public yii\base\View|yii\web\View getView ( )
return yii\base\View|yii\web\View

The view object that can be used to render views or view files.

getViewPath() public method

Returns the directory containing view files for this controller.

The default implementation returns the directory named as controller $id under the $module's $viewPath directory.

public string getViewPath ( )
return string

The directory containing the view files for this controller.

render() public method

Renders a view and applies layout if available.

The view to be rendered can be specified in one of the following formats:

  • path alias (e.g. "@app/views/site/index");
  • absolute path within application (e.g. "//site/index"): the view name starts with double slashes. The actual view file will be looked for under the view path of the application.
  • absolute path within module (e.g. "/site/index"): the view name starts with a single slash. The actual view file will be looked for under the view path of $module.
  • relative path (e.g. "index"): the actual view file will be looked for under $viewPath.

To determine which layout should be applied, the following two steps are conducted:

  1. In the first step, it determines the layout name and the context module:
  • If $layout is specified as a string, use it as the layout name and $module as the context module;
  • If $layout is null, search through all ancestor modules of this controller and find the first module whose layout is not null. The layout and the corresponding module are used as the layout name and the context module, respectively. If such a module is not found or the corresponding layout is not a string, it will return false, meaning no applicable layout.
  1. In the second step, it determines the actual layout file according to the previously found layout name and context module. The layout name can be:
  • a path alias (e.g. "@app/views/layouts/main");
  • an absolute path (e.g. "/main"): the layout name starts with a slash. The actual layout file will be looked for under the layout path of the application;
  • a relative path (e.g. "main"): the actual layout file will be looked for under the layout path of the context module.

If the layout name does not contain a file extension, it will use the default one .php.

public string render ( $view, $params = [] )
$view string

The view name.

$params array

The parameters (name-value pairs) that should be made available in the view. These parameters will not be available in the layout.

return string

The rendering result.

throws yii\base\InvalidParamException

if the view file or the layout file does not exist.

renderContent() public method (available since version 2.0.1)

Renders a static string by applying a layout.

public string renderContent ( $content )
$content string

The static string being rendered

return string

The rendering result of the layout with the given static string as the $content variable. If the layout is disabled, the string will be returned back.

renderFile() public method

Renders a view file.

public string renderFile ( $file, $params = [] )
$file string

The view file to be rendered. This can be either a file path or a path alias.

$params array

The parameters (name-value pairs) that should be made available in the view.

return string

The rendering result.

throws yii\base\InvalidParamException

if the view file does not exist.

renderPartial() public method

Renders a view without applying layout.

This method differs from render() in that it does not apply any layout.

public string renderPartial ( $view, $params = [] )
$view string

The view name. Please refer to render() on how to specify a view name.

$params array

The parameters (name-value pairs) that should be made available in the view.

return string

The rendering result.

throws yii\base\InvalidParamException

if the view file does not exist.

run() public method

Runs a request specified in terms of a route.

The route can be either an ID of an action within this controller or a complete route consisting of module IDs, controller ID and action ID. If the route starts with a slash '/', the parsing of the route will start from the application; otherwise, it will start from the parent module of this controller.

See also runAction().

public mixed run ( $route, $params = [] )
$route string

The route to be handled, e.g., 'view', 'comment/view', '/admin/comment/view'.

$params array

The parameters to be passed to the action.

return mixed

The result of the action.

runAction() public method

Runs an action within this controller with the specified action ID and parameters.

If the action ID is empty, the method will use $defaultAction.

See also createAction().

public mixed runAction ( $id, $params = [] )
$id string

The ID of the action to be executed.

$params array

The parameters (name-value pairs) to be passed to the action.

return mixed

The result of the action.

throws yii\base\InvalidRouteException

if the requested action ID cannot be resolved into an action successfully.

setView() public method

Sets the view object to be used by this controller.

public void setView ( $view )
$view yii\base\View|yii\web\View

The view object that can be used to render views or view files.

setViewPath() public method (available since version 2.0.7)

Sets the directory that contains the view files.

public void setViewPath ( $path )
$path string

The root directory of view files.

throws yii\base\InvalidParamException

if the directory is invalid

Event Details

EVENT_AFTER_ACTION event of type yii\base\ActionEvent

An event raised right after executing a controller action.

EVENT_BEFORE_ACTION event of type yii\base\ActionEvent

An event raised right before executing a controller action. You may set yii\base\ActionEvent::$isValid to be false to cancel the action execution.