FuelPHP - Presenters

FuelPHP 在控制器之后提供了一个额外的层来生成视图。一旦控制器处理完输入并完成业务逻辑,它就会将控制权发送给 Presenter,后者负责处理额外的逻辑,例如从数据库获取数据、设置视图数据等,然后调用 View 对象。

我们可以使用 Presenter 类呈现视图,如下所示 −

fuel/app/classes/controller/employee.php

public Controller_Employee extends Controller { 
   public function action_welcome() { 
      return Presenter::forge('employee/hello'); 
   } 
}

Presenter 类的默认位置是 fuel/app/classes/presenter/。以下是一个简单的示例。

fuel/app/classes/presenter/employee/hello.php

<?php  
   class Presenter_Employee_Hello extends Presenter { 
      public function view() { 
         $this->name = Request::active()->param('name', 'World'); 
      } 
   } 

上述演示器类的视图文件解析为相对于 views 文件夹的 employee/hello.php,这与指定一致。

fuel/app/views/employee/hello.php

<h3>Hi, <?php echo $name; ?></h3> 

最后,更改路由以匹配员工的欢迎操作,如下所示 −

fuel/app/config/routes.php

'employee/hello(/:name)?' => array('employee/welcome', 'name' => 'hello'),

现在,请求 URL,http://localhost:8080/employee/hello/Jon 呈现以下结果。

结果

Presenter View