Presenter 类別

The Presenter class acts as an object wrapper for "views", and is used to abstract all logic related to the view away from the controller. 閱讀更多關於使用表现控件。 就像 Controller,Presenter 支援 before()after() 方法,你可以用來which you can use for code generic to all methods for view prepping.

forge($presenter, $method = 'view', $auto_filter = null, $view = null)

forge 方法回傳一个新的 Presenter 物件。

靜態
參数
參数 預設 描述
$presenter 必要 表现控件的名稱,以及預設其關聯的檢視,使用 View 表示。
$method
'view'
Name of the presenter method that will prep the View for rendering. You can have multiple prep methods defined in the Presenter, for example to generate different layouts of the same view.
$auto_filter
null
set to true or false to set auto encoding, defaults to main config setting (app/config/config.php)
$view
null
回傳 一个新的 Presenter 物件
範例
// 会为 APPPATH/views/admin/index.php 檢視档案
// 建立一个 Presenter 物件
// 使用在 APPPATH/classes/presenter/admin/index.php 中的 Presenter_Admin_Index 类別

$presenter = Presenter::Forge('admin/index');

// 使用 presenter 中的 custom() 方法來呈现不同的檢視
$presenter = Presenter::Forge('admin/index', 'custom');

// 使用一个自訂檢視
$presenter = Presenter::Forge('admin/index', 'custom', null, 'admin/different/view');

// 或甚至一个自訂的檢視物件
$view = View::forge('admin/different/view', array(
	'menu' => $menu,
	'articles' => $articles,
	'footer_links' => $footer_links,
));

$presenter = Presenter::Forge('admin/index', 'custom', null, $view);

get_view()

The get_view method returns the View instance associated with the Presenter object.

靜態
參数
回傳 關聯的 View 物件
範例
// 建立一个表现控件實例
$presenter = Presenter::Forge('admin/index');

// and the view associated with it
$view = $presenter->get_view();

view()

The view method is the default method which is called when the Presenter is rendered. It contains the logic to prep the view for rendering.

靜態
參数
範例 參閱 Presenter 預覽頁面。

A presenter can contain multiple prepping methods, which are used when you need multiple sets of logic for generating the view. You could for example have a custom method that generates the view without headers and footers, or one that creates a custom view optimized for mobile devices. It allows you to keep the controller generic, it doesn't need to know what output has to be generated by the presenter.

View 物件相容性

The Presenter class is interchangeable with the View class in your code. This means that if you start with Views, and later realize you need to have additional view prepping logic and you want to use a Presenter instead, you don't have to change your controller code, other than forging a Presenter instead of a View.

To support this, the Presenter exposes the set(), set_safe(), bind(), auto_filter() and render() methods of the associated View object. Is also has magic getter and setters to access and set properties on the associated View object.

The Presenter doesn't support the static methods set_global() and bind_global(), if you need global variables for your views, you still have to set them on the View class. For the Presenter, this is transparent.

If you want to extend the Presenter to be able to swap View instances after the Presenter object has been created, know that the presenter doesn't have it's own data container. Instead, it uses the associated View object to store all data, which means that if you swap that View object by a new one, you lose all variables set on it!