The Yaf_Controller_Abstract class

(Yaf >=1.0.0)

簡介

Yaf_Controller_Abstract 是Yaf的MVC體系的核心部分。 MVC是指Model-View-Controller, 是一個(gè)用于分離應(yīng)用邏輯和表現(xiàn)邏輯的設(shè)計(jì)模式。

每個(gè)用戶自定義controller都應(yīng)當(dāng)繼承Yaf_Controller_Abstract

你會(huì)發(fā)現(xiàn)在你自己的controller中無法定義__construct方法。因此,Yaf_Controller_Abstract 提供了一個(gè)魔術(shù)方法Yaf_Controller_Abstract::init()。

如果在你自己的controller里面已經(jīng)定義了一個(gè)init()方法,當(dāng)你的controller被實(shí)例化的時(shí)候,它將被調(diào)用。

Action可能需要參數(shù),當(dāng)一個(gè)請(qǐng)求來到的時(shí)候,在路由中如果請(qǐng)求的參數(shù)有相同名稱的變量(例如: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

你也可以通過使用這個(gè)值和 Yaf_Action_Abstract 在一個(gè)單獨(dú)的PHP腳本中定義action函數(shù)。

示例 #1 在一個(gè)獨(dú)立的文件中定義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

當(dāng)前的請(qǐng)求實(shí)例

_response

_invoke_args

_view

視圖引擎

目錄