略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Yaf_Controller_Abstract

2024-04-26

The Yaf_Controller_Abstract class

(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方法(see 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 $module,
    string $controller = ?,
    string $action = ?,
    array $paramters = ?
): void
public getInvokeArg(string $name): void
public getInvokeArgs(): void
public getModuleName(): string
public getName(): string
public getViewpath(): void
public init(): void
public initView(array $options = ?): void
public redirect(string $url): void
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 indexAction($name$id) {
       
assert($name == $this->getRequest()->getParam("name"));
       
assert($id   == $this->_request->getParam("id"));
    }
}
?>

示例 #2 Dummy_action.php

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

模块名

_name

_request

当前的请求实例

_response

_invoke_args

_view

视图引擎

目录

add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/class.yaf-controller-abstract.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3