PHP Класс yii\base\Controller

С версии: 2.0
Автор: Qiang Xue ([email protected])
Наследование: extends Component, implements yii\base\ViewContextInterface
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$action the action that is currently being executed. This property will be set by Controller::run when it is called by Application to run an action.
$defaultAction the ID of the action that is used when the action ID is not specified in the request. Defaults to 'index'.
$id the ID of this controller.
$layout the name of the layout to be applied to this controller's views. This property mainly affects the behavior of Controller::render. Defaults to null, meaning the actual layout value should inherit that from [[module]]'s layout value. If false, no layout will be applied.
$module Module the module that this controller belongs to.

Открытые методы

Метод Описание
__construct ( string $id, Module $module, array $config = [] )
actions ( ) Declares external actions for the controller.
afterAction ( Action $action, mixed $result ) : mixed This method is invoked right after an action is executed.
beforeAction ( Action $action ) : boolean This method is invoked right before an action is executed.
bindActionParams ( Action $action, array $params ) : array Binds the parameters to the action.
createAction ( string $id ) : Action Creates an action based on the given action ID.
findLayoutFile ( View $view ) : string | boolean Finds the applicable layout file.
getModules ( ) : Module[] Returns all ancestor modules of this controller.
getRoute ( ) : string Returns the route of the current request.
getUniqueId ( ) : string Returns the unique ID of the controller.
getView ( ) : View | View Returns the view object that can be used to render views or view files.
getViewPath ( ) : string Returns the directory containing view files for this controller.
render ( string $view, array $params = [] ) : string Renders a view and applies layout if available.
renderContent ( string $content ) : string Renders a static string by applying a layout.
renderFile ( string $file, array $params = [] ) : string Renders a view file.
renderPartial ( string $view, array $params = [] ) : string Renders a view without applying layout.
run ( string $route, array $params = [] ) : mixed Runs a request specified in terms of a route.
runAction ( string $id, array $params = [] ) : mixed Runs an action within this controller with the specified action ID and parameters.
setView ( View | View $view ) Sets the view object to be used by this controller.
setViewPath ( string $path ) Sets the directory that contains the view files.

Описание методов

__construct() публичный Метод

public __construct ( string $id, Module $module, array $config = [] )
$id string the ID of this controller.
$module Module the module that this controller belongs to.
$config array name-value pairs that will be used to initialize the object properties.

actions() публичный Метод

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, php 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 actions ( )

afterAction() публичный Метод

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: php public function afterAction($action, $result) { $result = parent::afterAction($action, $result); your custom code here return $result; }
public afterAction ( Action $action, mixed $result ) : mixed
$action Action the action just executed.
$result mixed the action return result.
Результат mixed the processed action result.

beforeAction() публичный Метод

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: php 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 beforeAction ( Action $action ) : boolean
$action Action the action to be executed.
Результат boolean whether the action should continue to run.

bindActionParams() публичный Метод

This method is invoked by Action when it begins to run with the given parameters.
public bindActionParams ( Action $action, array $params ) : array
$action Action the action to be bound with parameters.
$params array the parameters to be bound to the action.
Результат array the valid parameters that the action can run with.

createAction() публичный Метод

The method first checks if the action ID has been declared in Controller::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 InlineAction representing that method will be created and returned.
public createAction ( string $id ) : Action
$id string the action ID.
Результат Action the newly created action instance. Null if the ID doesn't resolve into any action.

findLayoutFile() публичный Метод

Finds the applicable layout file.
public findLayoutFile ( View $view ) : string | boolean
$view View the view object to render the layout file.
Результат string | boolean the layout file path, or false if layout is not needed. Please refer to [[render()]] on how to specify this parameter.

getModules() публичный Метод

The first module in the array is the outermost one (i.e., the application instance), while the last is the innermost one.
public getModules ( ) : Module[]
Результат Module[] all ancestor modules that this controller is located within.

getRoute() публичный Метод

Returns the route of the current request.
public getRoute ( ) : string
Результат string the route (module ID, controller ID and action ID) of the current request.

getUniqueId() публичный Метод

Returns the unique ID of the controller.
public getUniqueId ( ) : string
Результат string the controller ID that is prefixed with the module ID (if any).

getView() публичный Метод

The Controller::render, Controller::renderPartial and Controller::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 getView ( ) : View | View
Результат View | View the view object that can be used to render views or view files.

getViewPath() публичный Метод

The default implementation returns the directory named as controller [[id]] under the [[module]]'s [[viewPath]] directory.
public getViewPath ( ) : string
Результат string the directory containing the view files for this controller.

render() публичный Метод

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 [[Application::viewPath|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 [[Module::viewPath|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 [[Module::layout|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. 2. 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 [[Application::layoutPath|layout path]] of the application; - a relative path (e.g. "main"): the actual layout file will be looked for under the [[Module::layoutPath|layout path]] of the context module. If the layout name does not contain a file extension, it will use the default one .php.
public render ( string $view, array $params = [] ) : string
$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.
Результат string the rendering result.

renderContent() публичный Метод

Renders a static string by applying a layout.
С версии: 2.0.1
public renderContent ( string $content ) : string
$content string the static string being rendered
Результат 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() публичный Метод

Renders a view file.
public renderFile ( string $file, array $params = [] ) : string
$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.
Результат string the rendering result.

renderPartial() публичный Метод

This method differs from Controller::render in that it does not apply any layout.
public renderPartial ( string $view, array $params = [] ) : string
$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.
Результат string the rendering result.

run() публичный Метод

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.
См. также: runAction()
public run ( string $route, array $params = [] ) : mixed
$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.
Результат mixed the result of the action.

runAction() публичный Метод

If the action ID is empty, the method will use [[defaultAction]].
См. также: createAction()
public runAction ( string $id, array $params = [] ) : mixed
$id string the ID of the action to be executed.
$params array the parameters (name-value pairs) to be passed to the action.
Результат mixed the result of the action.

setView() публичный Метод

Sets the view object to be used by this controller.
public setView ( View | View $view )
$view View | View the view object that can be used to render views or view files.

setViewPath() публичный Метод

Sets the directory that contains the view files.
С версии: 2.0.7
public setViewPath ( string $path )
$path string the root directory of view files.

Описание свойств

$action публичное свойство

the action that is currently being executed. This property will be set by Controller::run when it is called by Application to run an action.
public $action

$defaultAction публичное свойство

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

$id публичное свойство

the ID of this controller.
public $id

$layout публичное свойство

the name of the layout to be applied to this controller's views. This property mainly affects the behavior of Controller::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 $layout

$module публичное свойство

the module that this controller belongs to.
public Module,yii\base $module
Результат Module