jueves, 30 de enero de 2014

Disallowed Key Characters WTF??!!

For any weird reason, when I send some forms  this message is displayed:

Disallowed Key Characters

WTF? What I changed?? I don't know. I only know one thing: when this message appeared, I had to start debugging the FRAMEWORK, not my code, not your code, just the framework.

I found the problem in file system/core/Input.php, line 727, function _clean_input_keys{}

When I have a POST field containing "-", this doesnt match the preg_match("/^[a-z0-9:_\/-]+$/i", $str), I dont know why because the "-" IS HERE!!! then ...

I run this: var_dump(preg_match("/^[a-z0-9:_\/-]+$/i", '-')); die(); and of course was TRUE.

Conclusion: I don't know WTF is happening (It's like a devil '-' different from '-'), but, if you are seeing "Disallowed Key Characters" message and you have a post field called, for example "login-submit", rename this field to "login_submit" and maybe all the things start working again.

domingo, 12 de enero de 2014

Codeigniter dinamic route from database

What if we want to route from a db table? I search all the web and I dont find a solution that makes me happy, so, I build one.

File /core/MY_Router.php: we extend the core router to add the database functionality.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
CUSTOM ROUTER FUNCTION TO CHECK FOR CITY SLUG PLUS CONTROLLERS */
 class MY_Router extends CI_Router{

public function __construct(){
        parent::__construct();
        $this->config =& load_class('Config', 'core');
        $this->uri =& load_class('URI', 'core');

        require_once(BASEPATH.'database/DB'.EXT);
        $this->db = DB();
    }


public function _set_routing(){

$db_routes = $this->db
->where('active', 1)
->get('routes')
->result();

foreach ($db_routes as $route) {

$this->routes[$route->slug] = 'coupons/index/' . $city->slug;
$this->routes[$route->slug . '/(:any)'] = 'coupons/index/' . $city->slug . '/$1';
}

parent::_set_routing();
}
}

We need to change one character in system/core/Router.php to avoid overwrite the routes array.

In line 141 change this:
$this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route; 

to this:
$this->routes += ( ! isset($route) OR ! is_array($route)) ? array() : $route;

(NOTE THE + SIGN)

That's all. Efective and Beautiful.