FuelPHP 控制器

fuelphp 控制器

 

控制器负责处理进入 fuelphp 应用程序的每个请求。根据 fuelphp,控制器位于 fuel/app/classes/controller/。让我们首先创建一个员工控制器。

 

employee.php

  
   class controller_employee extends controller { 
      public function action_home() { 
         echo "fuelphp-employee application!"; 
      }  
      
      public function action_index() { 
         echo "this is the index method of employee controller"; 
      } 
   } </pre--> 

 

<h2>控制器方法</h2>

控制器使用其 <strong>action</strong>_ 方法之一处理网络请求。我们可以根据应用程序的要求创建尽可能多的 <em>action_</em> 方法。默认的 <em>action_</em> 方法是 <em>action_index</em>。 <em>action_index</em> 方法可以被以下任一网址调用。

http://localhost:8080/employee/index
http://localhost:8080/employee/

 

<h3>结果</h3>

<img src="/public/core/edit/php/../attached/20231217205855_98670.jpg" alt="" border="0" />

让我们在 <strong>employee</strong> 应用程序中创建一个新的 <strong>action</strong> 方法 <em>action_show</em>。

  
   class controller_employee extends controller { 
      public function action_home() { 
         echo "fuelphp-employee application!"; 
      }  
      public function action_index() { 
         echo "this is the index method of employee controller"; 
      }  
      public function action_show() { 
         echo "this is the show method of employee controller"; 
      } 
   } </pre--> 

<em>action_show</em> 方法可以使用 t 调用他下面的网址。

http://localhost:8080/home/show

 

<h3>结果</h3>

<img src="/public/core/edit/php/../attached/20231217205928_57351.jpg" alt="" border="0" />

 

<h2>before( ) 方法</h2>

我们可以在控制器中 <strong>之前</strong>创建一个方法。此方法将在每个 <em>action_</em> 方法调用之前执行。如果该方法不存在,则不会调用它。该方法帮助我们编写常见的操作,例如登录检查、默认数据获取等。

让我们创建一个 <em>before</em> 方法并打印一条简单的文本消息。

public function before() { 
   echo "this message comes from <em>before()</em> method
"; 
} 

 

<h3>带有动作前的索引页</h3>

<img src="/public/core/edit/php/../attached/20231217210006_15993.jpg" alt="" border="0" />

 

<h3>显示有动作前的页面</h3>

<img src="/public/core/edit/php/../attached/20231217210046_70232.jpg" alt="" border="0" />

 

<h2>after() 方法</h2>

<strong>after()</strong> 方法类似于 <strong>before()</strong> 方法,但在调用 <em>action_</em> 方法之后执行。 <em>after()</em> 方法将 <em>response</em> 作为输入并返回 <em>response</em> 对象。

public function after($response) { 
   if ( ! $response instanceof response) { 
      $response = \response::forge($response, $this->response_status); 
   } 
   return $response; 
} 

如果输入为 null 或不是响应对象,则使用 response 的伪造方法创建一个新的 <em>response</em> 对象并返回它。我们将在后续章节中详细学习 response 类。

 

<h2>扩展控制器</h2>

我们可以从另一个控制器扩展一个控制器。以下是基本语法。

class controller_employee extends controller_welcome { 
   // controller methods 
} 

这将有助于分享方法。

 

<h2>生成控制器</h2>

fuel 可以选择使用 oil 命令生成控制器。以下是语法。

 

<h3>语法</h3>
oil g controller <controller-name> </controller-name>

 

<h3>示例</h3>
oil g controller sample

执行上述命令后,您将看到以下响应。

 

<h3>结果</h3>
creating view: /path/to/project/fuel/app/views/template.php 
creating view: /path/to/project/fuel/app/views/sample/index.php 
creating controller: /path/to/project/fuel/app/classes/controller/sample.php 

 

<h2>控制器类型</h2>

fuelphp 为各种目的提供了不同类型的控制器。它们如下:

<ul> <li>基本控制器</li> <li>模板控制器</li> <li>休息控制器</li> <li>混合控制器</li> </ul>

 

<h3>基础控制器</h3>

controller 是 fuelphp 中可用的所有不同类型控制器的基础控制器。它提供了处理 web 请求所需的所有基本功能。它支持请求、响应、会话等。除非另有说明,否则我们将在所有示例中使用它。

 

<h3>模板控制器</h3>

模板控制器是基本控制器的扩展。它有模板支持,预定义 before() 和 after() 方法。基本上,它可用于将视图包装在带有页眉、页脚、侧边栏等的布局中。要创建模板控制器,我们需要扩展 <em>controller_template</em> 类。默认情况下,扩展 <em>controller_template</em> 的类的所有方法都需要使用模板。

定义如下。

class controller_employee extends controller_template { 
   public function action_index() { 
      // add methods 
   } 
}

我们将在视图章节讨论更多关于模板控制器的内容。

 

<h3>休息控制器</h3>

rest controller 是 base controller 的扩展。它具有对 rest api 编程的预定义支持。这将使您能够轻松构建 api。

要创建 rest 控制器,您需要扩展 <em>controller_rest</em> 类。其定义如下。

class controller_employee extends controller_rest { 
   public function action_index() { 
      // add methods 
   } 
}

我们将在 ajax 章节中讨论更多关于 rest 控制器的内容。

 

<h3>混合控制器</h3>

混合控制器在单个基本控制器中执行 rest 控制器和模板控制器的功能。

<h3><a href="/s7900103/fuelphp 路由.html">下一节:fuelphp 路由</a></h3> <a class="bottom-summary-prompt" href="/php/php_sz/153.html"><h3>fuelphp 教程</h3> </a>
相关文章