Code Igniter: $autoload ('Libraries') Array ('Database')

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Code igniter

For autoloding database

$autoload['libraries'] = array('database') ;.
Else manually call $this->load->database();

http://www.formget.com/codeigniter-form-input/

layout pattern in:


http://a32.me/2012/09/using-layout-pattern-with-codeigniter/

validation in CI:
http://www.codeigniter.com/user_guide/libraries/form_validation.html#saving-sets-of-validation-rules-to-a-configfile

http://www.formget.com/form-validation-using-codeigniter/

Passing data from controller to view:


The data pass from controller to view will never be access by variable name rather it access by the index in the
variable as variable
// in controller

$data = array(index=>array(a=>b));
$this->load->view(view_page, $data);
// in view
Print_r($index);

Getting router in a hook class file

$this->router->fetch_class();
Extend CI_Controller and this should work.

Redirect to ssl in codeigniter :HTTPS :- Full name is (hyper text transfer protocol secure). Its a extended or you can say secure
version of the HTTP(hyper text transfer protocol). If you have https enabled on your server you will get
a lock icon in address bar of browser.
For encryption of data its used SSL(secure sockets layer) certificates to install on your server. Most
uses of https to securely transmit data with ssl.
So most of sites need ssl or https url. we can easily manage redirection to https url in codeigniter using
hooks. its a one more good feature of CI. We need to create some lines of code to get ssl url or
redirect to https url in codeigniter. Default codeigniter not have this type of functionality so we need to
force redirect to ssl url. below we will see how to use ssl redirect with codeigniter.
1. Config changes :- Go to application/config/config.php and enable or set hooks to true.

$config['enable_hooks'] = TRUE;

2. create a new file named hooks.php in application/config/hooks.php and add below code in
hooks.php:-

$hook['post_controller_constructor'][] = array(

'function' => 'redirect_ssl',

'filename' => 'ssl.php',

'filepath' => 'hooks'

);

3. Now create a new directory with named hooks under application directory and then create new
file named ssl.php in application/hooks/ssl.php
and add below code to ssl.php :-

function redirect_ssl() {

$CI =& get_instance();

$class = $CI->router->fetch_class();

$exclude = array('client'); // add more controller name to exclude ssl.

if(!in_array($class,$exclude)) {

// redirecting to ssl.

$CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);

if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());

10

else {

11

// redirecting with no ssl.

12

$CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);


if ($_SERVER['SERVER_PORT'] == 443) redirect($CI->uri->uri_string());

13
}

14
15

Now check your site you will get https in url. redirect to https url in codeigniter using hooks is a good
method rather than htaccess.

Menu active

http://tutsnare.com/add-active-class-to-menu-in-codeigniter/

Prevent fatal error php in codeigniter


http://thecancerus.com/how-to-catch-php-fatal-error-in-codeigniter/

Codeigniter - Hook to log GET / POST REQUESTS

As mentionned by Patrick Savalle you should use hooks. Use the post_controller_constructorhook so you can
use all other CI stuff.
1) In ./application/config/config.php set $config['enable_hooks'] = TRUE
2) In ./application/config/hooks.php add the following hook
$hook['post_controller_constructor'] = array(
'class' => 'Http_request_logger',
'function' => 'log_all',
'filename' => 'http_request_logger.php',
'filepath' => 'hooks',
'params' => array()
);

3) Create the file ./application/hooks/http_request_logger.php and add the following code as example.
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Http_request_logger {
public $CI;
public function log_all() {
$this->CI = & get_instance();
log_message('info', 'GET --> ' . var_export($this->CI->input->get(null), true));
log_message('info', 'POST --> ' . var_export($this->CI->input->post(null), true));
log_message('info', '$_SERVER -->' . var_export($_SERVER, true));
}
}

I've tested it and it works for me (make sure you have logging activated in your config file).

FILE upload problem


Figured out the idiotic problem.
I was autoloading the library and some how when I was trying to initialize the configuration by $this->load>library('upload', $config); it wouldn't do so.
Instead I put my config files in config/upload.php
The other method to do so would have been $this->upload->initialize($config);

Form validation custom message


http://stackoverflow.com/questions/12473124/form-validation-custom-message-in-codeigniter

Form validation not working due to Hook


When extending the CI_controller in Hooks the form validation will not work
http://stackoverflow.com/questions/9545967/code-igniter-form-validation-no-error-messages

You might also like