目录

FuelPHP - 演示者( Presenters)

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

我们可以使用Presenter类渲染视图,如下所示 -

fuel/app/classes/controller/employee.php

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

演示者类的默认位置是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呈现以下结果。

结果 (Result)

演示者视图
↑回到顶部↑
WIKI教程 @2018