CakeFest 2024: The Official CakePHP Conference

Yaf_Controller_Abstract 类

(Yaf >=1.0.0)

简介

Yaf_Controller_Abstract 是 Yaf 系统的 MVC 体系的核心部分。MVC 是指 Model-View-Controller,是一个用于分离应用逻辑和表现逻辑的设计模式。

每个自定义 controller 都应当继承 Yaf_Controller_Abstract

自定义 controller 中无法定义 __construct 方法。因此,Yaf_Controller_Abstract 提供了一个魔术方法 Yaf_Controller_Abstract::init()

如果自定义 controller 里已经定义了 init() 方法,当实例化 controller 的时候,它将被调用。

Action 可能需要参数,当请求来到的时候,在路由中如果请求的参数有相同名称的变量(例如:Yaf_Request_Abstract::getParam()),Yaf 将把他们传递给 action 方法(参阅 Yaf_Action_Abstract::execute())。

注意:

这些参数是直接获取的,没有过滤,使用他们之前应该仔细处理。

类摘要

abstract class Yaf_Controller_Abstract {
/* 属性 */
public $actions;
protected $_module;
protected $_name;
protected $_request;
protected $_response;
protected $_invoke_args;
protected $_view;
/* 方法 */
final private __construct()
protected display(string $tpl, array $parameters = ?): bool
public forward(string $action, array $paramters = ?): bool
public getInvokeArg(string $name): void
public getName(): string
public init(): void
public initView(array $options = ?): void
public redirect(string $url): bool
protected render(string $tpl, array $parameters = ?): string
public setViewpath(string $view_directory): void
}

属性

actions

也可以通过使用此属性和 Yaf_Action_Abstract 在一个单独的 PHP 脚本中定义 action 方法。

示例 #1 在单独的文件中定义 action

<?php
class IndexController extends Yaf_Controller_Abstract {
protected
$actions = array(
/** now dummyAction is defined in a separate file */
"dummy" => "actions/Dummy_action.php",
);

/* action method may have arguments */
public function indexAction($name, $id) {
/* $name and $id are unsafe raw data */
assert($name == $this->getRequest()->getParam("name"));
assert($id == $this->_request->getParam("id"));
}
}
?>

示例 #2 Dummy_action.php

<?php
class DummyAction extends Yaf_Action_Abstract {
/* an action class shall define this method as the entry point */
public function execute() {
}
}
?>

_module

模块名

_name

控制器名

_request

当前请求实例

_response

当前响应对象

_invoke_args

_view

视图引擎

目录

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top