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

表现控件 - 一般

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

表现控件

表现控件是什幺?

A Presenter is a class that contains the logic that is needed to generate your view (or views). When the controller is done with your user input and is done with whatever actions it needed to take, it turns execution over to the Presenter to retrieve and process whatever data is needed for the view. A Presenter shouldn't do any data manipulation but can contain database calls and any other retrieval or preparation operations needed to generate the View's data.

表现控件是选择性的,如果你不需要它们,你可以直接使用 Views,并保持在控制器中预处理逻辑。

建立一个表现控件

首先我们在 APPPATH/classes/presenter/index.php 建立一个空的 Presenter 类别:

class Presenter_Index extends Presenter
{
}

Then you create the view that is associated with the presenter in app/views/index.php:

<h1><?php echo $title; ?></h1>

<ul>
<?php
	foreach ($articles as $a)
	{
		echo '<li>'.$a->title.'</li>';
	}
?>
</ul>

关于检视名称
A Presenter and its view are by default expected to share the same name. Thus a Presenter Presenter_Index expects the view to be in app/views/index.php. And underscores work here the same as with classes, which means that the view for Presenter_Some_Thing is expected to be in app/views/some/thing.php.
This default can be overwritten by setting a non-static $_view property in your Presenter with the View name (without it's suffix), or passing a custom View name when forging the Presenter.

And last we'll create the Presenter from the controller:

$presenter = Presenter::forge('index');

Now we have everything setup; however, there is still no data passed to the view. It still needs to get a $title string and $articles array passed to it. We do this by adding a view() method to the Presenter which will assign this data:

class Presenter_Index extends Presenter
{

	public function view()
	{
		$this->title = 'Testing this Presenter thing';

		$this->articles = Model_Articles::find('all');
	}
}

你就大功告成了。

In your code, Views and Presenters are interchangeable. You can return Presenters from your controller actions, you can set a Presenter as a Theme partial, or assign it to a section of your page template. The basic API of the Presenter is compatible with the View. This makes it easy to swap a View for a Presenter in your code without having to do a major code overhaul.

传递函式到检视

To pass a View specific function from your Presenter to your View, you use Closures:

// 在 Presenter 中
class Presenter_Index extends Presenter
{

	public function view()
	{
		$this->echo_upper = function($string) { echo strtoupper($string); };
	}
}

// 然后你可以在你的检视使用:
$echo_upper('this string'); // 输出:"THIS STRING"

安全性

它与 View 运作相同。这意味着任何在 Presenter 的设定将被输出编码, 只要你不关掉它的话。你可以直接在 Presenter 使用相同的 set($name, $value, $encode) 方法, 就像你在 View 使用那样。更多关于此在 View 的安全性章节

进阶用法

更多方法

If there are different ways of parsing the same View, you can add multiple methods to the Presenter other than the default view() method. To use those, you need to add the method's name as the second parameter to the Presenter::forge() method:

// will call other_method() upon the Presenter from the above example
$presenter = Presenter::forge('index', 'other_method');

Before 和 after 方法

如果你需要让一些资料添加到 Presenter 中的所有方法,你可以添加一个 before()after() 方法。就像你在 Controllers 做的那样。

变更检视

By default, the $this->_view gets a View object assigned to it. You can replace this object by making your own set_view() method in the Presenter and setting the $this->_view to an object of your choosing.
However, this object must allow you to set properties on it (which are used as the template data) and must have a __toString() magic method that will render and return the parsed contents. In other works, it must be compatible with the behaviour of the View class.
The name of the expected view is available in the $this->_view property.

Using a different view file, or an existing View object

You can also tell the Presenter instance to use a different view, and not use the automatic mechanism for determining the view to load, by using the forth parameter of the forge() method:

// 使用 'other/index' 检视来代替 'index' 检视
$presenter = Presenter::forge('index', 'other_method', null, 'other/index');

// 你也可以直接传递一个 View 物件
$view = View::forge('other/index');
$presenter = Presenter::forge('index', 'other_method', null, $view);

存取检视

你可以使用 get_view() 方法在 Presenter 以外的地方存取 View 物件。

从其他命名空间或非 Presenter_ 前缀的地方使用 Presenter

如果你想要使用这些,你必须对 forge()使用完整的类别名称,包含命名空间。 在这些情况下,预设的命名往往会不如预期般运作, 所以鼓励设定 $_view 特性,或传递一个自订检视名称。