dongye1912 2017-05-23 03:35
浏览 11
已采纳

每次codeigniter都将自定义过滤器应用于$ _GET变量

I need to trim every $this->input->get('q', true); in my projects. is there a way to do this instead of adding trim() every time?

Naim Malek told me to use helper, but I don't quite understand how it would work in this case..

  • 写回答

1条回答 默认 最新

  • doulu5717 2017-05-23 03:55
    关注

    You can use hooks for trimming every 'q' get parameter.

    First enable hooks in application/config/config.php

    $config['enable_hooks'] = TRUE;
    

    Then create a file with custom name (example: Trim_hooks.php) in application/hooks and write below code in hook config file(application/config/hooks.php) file.

    $hook['post_controller_constructor'] = array(
        'class' => 'Trim_hook',
        'function' => 'run',
        'filename' => 'Trim_hooks.php',
        'filepath' => 'hooks',
    );
    

    At the end create Trim_hooks.php file in application/hooks:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Trim_hook
    {
      function run()
      {
        if (isset($_GET['q']))
        {
          $_GET['q'] = trim($_GET['q']);
        }
      }
    }
    

    Every time you have q parameter in GET, it's trimming after run controllers constructoror.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?