当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

Presenter - 类別

优质
小牛编辑
129浏览
2023-12-01

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!