Codeigniter configuration file load - php

I want to use a new file containing constants in Codeigniter so I created the file /config/labels.php
When I try to load it in my controller using $this->config->load('labels'); and it throws
application/config/labels.php file does not appear to contain a valid configuration array.
However when I put the code in the constants.php file everything works well.
labels.php
<?php
define('CLI_CIVILITE','Civilité');
define('CLI_NOM','Nom');
define('CLI_PRENOM','Prenom');

The config file should contain an array $config
Thats why it throws the error.
When the config class loads the config file, it checks if $config was set.
If not it will throw an error.
As far as I know there is no feature to load your own file with custom constants.
As of now you will have to add those constants to application/config/constants.php

In the constants file, define a variable like below:
$ORDER_STATUS = array(
'0' => 'In Progress',
'1' => 'On Hold',
'2' => 'Awaiting Review',
'3' => 'Completed',
'4' => 'Refund Requested',
'5' => 'Refunded');
Then, in the controller:
function __construct()
{
$this->config->load('$ORDER_STATUS');
}

write in your config example and save as banned_idcard.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config ['banned_idcard'] = array (
'23104013',
'2010201103',
'11106062',
);
And in ur Controoler
<?php
function __construct () {
$banned_idcards = $this->config->load('banned_idcard');
}

in your config /config/labels.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
'CLI_CIVILITE' => 'Civilité',
'CLI_NOM' => 'Nom',
'CLI_PRENOM' => 'Prenom'
);
in your controller:
$this->config->load('labels');
var_dump((array)$this->config); //show all the configs including those in the labels.php

Related

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 trim() every time?
Naim Malek told me to use helper, but I don't quite understand how it would work in this case..
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.

CodeIgniter hook not catching insert/update queries

I am trying to use post_system or post_controller hook to catch and log all insert and update queries in a database table. But, what happens is that the $queries = $CI->db->queries; statement doesn't seem to catch any insert or update statement at all. It only catches SELECT statements even when new data is inserted or old data is updated in respective views/controllers.
Here is my relevant code:
hooks.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
// also tried with post_controller hook
$hook['post_system'] = array(
'class' => 'Db_query_log',
'function' => 'log_db_queries',
'filename' => 'db_log.php',
'filepath' => 'hooks'
);
hooks/db_log.php
<?php
class Db_query_log {
function __construct() {
//nothing special
}
function log_db_queries() {
$CI = & get_instance();
$queries = $CI->db->queries;
foreach ($queries as $key => $query) {
echo $query . "<br>";
// all statements displayed are SELECT statements even for UPDATE and INSERT operations performed by controllers where data is actually changed
}
}
}
What could be the culprit here? Am I missing something or this hook simply ignores INSERT/UPDATE operations?
Any help would be appreciated, thanks!
Use post_controller instead of post_system :
$hook['post_controller'] = array(
'class' => 'Db_query_log',
'function' => 'log_db_queries',
'filename' => 'db_log.php',
'filepath' => 'hooks'
);
To achieve this open any helper file and put this code at the bottom
function log_que($sql) {
$filepath = APPPATH . 'logs/Query-log-' . date('Y-m-d') . '.php';
$handle = fopen($filepath, "a+");
fwrite($handle, $sql." \n Execution Time: ".date("Y-m-d H:i:s")."\n\n");
fclose($handle);
}
after this go to system/database/DB_driver.php and find function named "query"
and put this below code on top inside the function.
log_que($sql);'
then all the queries will save in a file inside the application/logs folder.
you can put if condition if you want to save only specific queries. LIKE
if(preg_match('/INSERT/',$sql)) {
fwrite($handle, $sql." \n Execution Time: ".date("Y-m-d H:i:s")."\n\n");
}

CodeIgniter loading doctypes config

When I try to load the application/config/doctype.php config file in codeigniter
it gives me the next error
Your application/config/doctypes.php file does not appear to contain a valid configuration array.
My code is (taken from an example at CI docs)
$this->config->load('doctypes', FALSE, TRUE);
$x = $this->config->item('html5','doctypes');
echo "<pre>";var_dump($x);die;//
Anyone knows why?
Regarding to system/helpers/html_helper.php, you can juste use :
include(APPPATH.'config/doctypes.php');
And if you want to understand, look in Config.php core file, row 132 :
include($file_path);
if ( ! isset($config) OR ! is_array($config))
{
if ($fail_gracefully === TRUE)
{
return FALSE;
}
show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
}
If you use $this->load->config($file), you must have a $config variable in your file.
The proper way is to load the HTML helper in your controller:
$this->load->helper('html');
Once loaded, one can access the doctypes like so:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
<?=doctype('html5');?>
<html lang="en">
<head>
...
From the documentation: http://www-scf.usc.edu/~adportno/apache/htdocs/adportno/hw5/user_guide/helpers/html_helper.html#doctype

Get data config return array

i have two php files config.php look like this:
config.php :
<?php
return array(
'name' => 'Demo',
'age' => 21,
'job' => 'Coder'
);
?>
in file index.php, i using file_get_contents for get data of file config.php
index.php:
$config = file_get_contents('config.php');
echo $config['name'];
but this is not working. somebody can help me?
You include code and not its output.
$config = file_get_contents('config.php');
Sends your file to PHP and generates the output and then sends it back to you, which is not what you have to do for code. You have to do
$config = include('config.php'); // or require / require_once / include_once
The function file_get_contents(file) reads the entire file into a string. That means that it will return a string represntation of the content of the file and NOT source code that you can just use in your main script.
Note: If the path to file is an absolute path, file_get_contents() will execute the php first and return the rendered output of the php file.
Since you want the source of 'config.php' to be available in 'index.php', you have to include it.
Just use include() or include_once().
config.php :
<?
$config = array(
'name' => 'Demo',
'age' => 21,
'job' => 'Coder'
);
?>
index.php:
include('config.php');//or include_once('config.php');
//include makes the content of 'config.php' available exactly as if it was written inside of the document iteself.
echo $config['name'];
Hope this helps!

Custom Libraries in code igniter and config files?

I have a library in code igniter that looks like class MyClass($options = array())
The file is Myclass.php
I have a file (config/Myclass.php) that looks like
$Myclass = array(
'something' => 'value',
'another' => 'value'
);
Which I thought should pass the $Myclass array in when I initialize my class, but apparently not?
What do I need to do to fix it?
AH I found the answer,
The array inside your config file must be called $config.
The name of the file must also be a lower case representation of the library file name.
e.g
LIB FILE: Somelibrary.php
LIB CONTENTS: class Somelibrary($options = array()){...
CONF FILE: somelibrary.php
CONF CONTENTS: $config = array('something' => 'value');
The way this usually works is that you pass in an array of options you wish to override, or pass in nothing to use the defaults.
var myObject = new MyClass(); // default settings
var myObject = new MyClass(array('something' => 'value2')); // override only "something"
Honestly, I wouldn't create your own file in config without a good reason; instead, just put the defaults in your class definition, and then override in your constructor:
class MyClass {
var $default_options = array(
'something' => 'value',
'another' => 'value',
);
var $options = array();
function MyClass($override_options = array())
{
$this->options = array_merge($this->default_options, $override_options);
// your code here...
}
}
Fast-forward to 2013, someone is still having problems with this. So my situation was the same but slightly different, so I thought I'd try to save someone else some time.
I was naming my config file after my extended class, that was wrong. The Config file should always be form_validation.php, (this is because eventually it is handed to the CI_Form_validation class and that's what it expects)
Thanks to the folks who answered this question above, I realized that I had to pass the config to the parent class and using codeigniter v2.1.3 I did it as follows:
public function __construct( $config = array() )
{
parent::__construct($config);
}

Categories