The Yaf_Controller_Abstract class

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

檢視引擎

目錄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *