CakeFest 2024: The Official CakePHP Conference

Yaf_Controller_Abstract クラス

(Yaf >=1.0.0)

はじめに

Yaf_Controller_Abstract は Yaf システムの中心となるクラスです。 MVC は Model-View-Controller の略で、 アプリケーションのロジックと表示のロジックを切り離すためのデザインパターンです。

すべてのカスタムコントローラは Yaf_Controller_Abstract を継承する必要があります。

カスタムコントローラでは __construct を定義できません。そのため、 Yaf_Controller_Abstract ではマジックメソッド Yaf_Controller_Abstract::init() を用意しています。

カスタムコントローラで init() メソッドを定義すると、 コントローラのインスタンスを作成するときにそれが呼ばれます。

アクションには引数を持たせることができます。 リクエストが来たときに、もしリクエストのパラメータ (Yaf_Request_Abstract::getParam() を参照ください) に同名の変数があれば、Yaf はアクションのメソッド (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

アクションメソッドを個別の PHP スクリプトとして定義することもできます。 そのときには、このプロパティと Yaf_Action_Abstract を利用します。

例1 別ファイルでのアクションの定義

<?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