dapidmini dapidmini - 1 year ago 46
PHP Question

apply custom filter to $_GET variables everywhere everytime codeigniter

I need to trim every

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

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

Answer Source

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:

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download