zoukankan      html  css  js  c++  java
  • 控制器[3]

    六.空操作

    空操作是指系统在找不到请求的操作方法时,会定位到空操作(_empty)方法来执行,利
    用这个机制,我们可以实现错误页面和一些 URL 的优化。

    在 WeiBo/Home/Controller/UserController.class.php 中的代码 为:

     1 <?php
     2 
     3 namespace HomeController;
     4 use ThinkController;
     5 
     6 class UserController extends Controller {
     7         public function index() {
     8             echo "this is index ";            
     9         }
    10 }

    这时如果url为: http://localhost/demo39/user/index 则输出为: this is index ,

    但如果url为: http://localhost/demo39/user/test 则

    这时为了不出现上图,则添加函数  _empty() 

    在 WeiBo/Home/Controller/UserController.class.php 添加如下代码:

    1 public n function _empty($name) {
    2  echo '找不到方法:'.$name;
    3 }

    这时当url为: http://localhost/demo39/user/test ,则输出为:

    七. 空控制器

    所谓空控制器,就是请求不到指定控制器时,调用一个专门的空控制器。利用这个机制,
    我们可以实现错误页面和一些 URL 的优化。

    这时在 WeiBo/Home/Controller/Controller 下新建 EmptyController.class.php ,与 IndexController.class.php 同级,

    代码为:

    1 <?php
    2 namespace HomeController;
    3 use ThinkController;
    4 
    5 class EmptyController extends Controller {
    6     public function index() {
    7         echo "找不到控制器:".CONTROLLER_NAME;
    8     }
    9 }

    这时url为: http://localhost/demo39/test ,输出为: 找不到控制器:Test ,就不会出现出错页面了。

    八.操作绑定到类

    ThinkPHP 提供了把每个操作方法定位到一个类的功能,即每个把层次分的更加细腻。

    在 WeiBo/Common/Conf/config.php 中进行配置:

    1 //设置操作绑定到类
    2 'ACTION_BIND_CLASS'=>

    然后,在 Controller 目录下建立 User 目录,在 User 目录建立 index.class.php,
    再建立 test.class.php。

    1 //index.class.php内代码
    2  namespace HomeControllerUser;
    3  use ThinkController;
    4  class index  extends Controller {
    5  public  function run() {
    6  echo 'User模块下的index类';
    7     }
    8 }
    1 //test.class.php内代码
    2  namespace HomeControllerUser;
    3  use ThinkController;
    4  class test  extends Controller {
    5  public  function run() {
    6  echo 'User模块下的test类';
    7 }
    8 }

    需要注意的是,在User目录下的 class.php 文件如 index.class.php 中的 index 中的i不要大写,否则会出错(调试了半天才发现是大写首字母I出的错)

    且 index.class.php 中的function必须是run() 函数。

    这时在 index.class.php 中插入其它代码如下:

    1 public  function abc() {
    2   echo 'User模块下的abc类';
    3  }

    是不起作用的。

    要对 index.class.php 中的run()函数增加其它功能的话可使用以下函数:

    1 //前置后置方法
    2  public  function _before_run() {
    3  echo 'before_'.ACTION_NAME;
    4 }
    5  public  function _after_run() {
    6 echo 'after_'.ACTION_NAME;
    7 }

    空方法,在User目录里建立一个 _empty.class.php 与 index.class.php 同级

    1 <?php
    2 namespace HomeControllerUser;
    3 use ThinkController;
    4 
    5 class _empty extends Controller{
    6     public function run() {
    7         echo "找不到方法:".ACTION_NAME;
    8     }
    9 }

    空控制器,可以创建一个目录 _empty 与 User 目录同级,然后建立  index.class.php 

    1 <?php
    2 namespace HomeController\_empty;
    3 use ThinkController;
    4 
    5 class _empty extends Controller{
    6     public    function run() {
    7         echo "找不到该控制器:".CONTROLLER_NAME;
    8     } 
    9 }
  • 相关阅读:
    Part 17 Consuming ASP NET Web Service in AngularJS using $http
    Part 16 ng include directive in AngularJS
    Part 15 AngularJS ng init directive
    文本框
    mint上部署lamp环境
    18种有效有趣的读书方法(转载)
    可扩展的架构设计
    iisreset和w3wp的关系
    Ajax请求中带有IPv6地址后的百分号的问题
    IE中JavaScript 的异常处理
  • 原文地址:https://www.cnblogs.com/jacson/p/4583761.html
Copyright © 2011-2022 走看看