Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter helpful hints
#11

[eluser]tonanbarbarian[/eluser]
just to add to lones comments
if you need to add a "global" variable at run time then use
Code:
$this->config->set_item('variable', $value);
You can then use lones code to retrieve the value at any other time
Just be aware that this does not save the value in any config files for later use
#12

[eluser]xwero[/eluser]
some hints :

- When you have to use an input value multiple times put it in a variable it saves typing and you only have to make one call to the input->post method.

- Put form validation and other form related actions in a separate library. It keeps your controller cleaner and you see patterns sooner
Code:
// controller
$this->load->library('formactions');
function index()
{
   if(array_key_exists('addbutton',$_POST))
   {
      list($msg,$redirect) = $this->formactions->add();
   }
   if(array_key_exists('updatebutton',$_POST))
   {
      list($msg,$redirect) = $this->formactions->update();
   }
   if(array_key_exists('deletebutton',$_POST))
   {
      list($msg,$redirect) = $this->formactions->delete();
   }
   if($redirect != '')
   {
       redirect($redirect);
   }
   else
   {
       $data['msg'] = $msg;
   }
   $this->load->view('someview',$data);
}
// library
class Formactions
{
   var $ci;
  
   function Formactions()
   {
      $this->ci =& get_instance();
   }
  
   function add()
   {
      $msg = '';
      $redirect = '';
      // validation
      if(!$this->ci->validation->run())
      {
         $msg = $this->ci->validation->errror_string;
      }
      else
      {
         // do something else (for example database action, manipulate image, write to file, ...)
         // redirect if needed
         $redirect = 'controller/success';
      }
      return array($msg,$redirect);
   }
}
#13

[eluser]frenzal[/eluser]
I can't take credit for this as I found it on the forum myself once, however it's very handy and I use it for all my projects for setting the base url:


$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

$config['base_url'] = $root;
#14

[eluser]Firestorm ZERO[/eluser]
I totally agree with Point 1. The first thing I do is put the application directory separate from the system directory. Really this should be the default IMO.
#15

[eluser]Isern Palaus[/eluser]
Hello,

I ever put the directory application out of the system, and the system out of the public_html. My structure is like this:

/domain_tld/application
/domain_tld/system
/domain_tld/public_html

Regards,
-- Isern Palaus
#16

[eluser]Lone[/eluser]
Yet Another Hint - Easy Array/Variable Output

From the previous CMS we built we had the following function to help us with out with viewing what is in an array or variable. It's not the prettiest looking output but saves a heap of time when developing.

We added it to CodeIgniter by making it a helper called 'debug_helper.php' and just autoload it.

Example
Code:
$people = array('John','Mary','Alberto');
p($people); // Will print_r onto screen with a <pre> tag

Helper Code
Code:
function p($var)
{
    if(is_array($var)) {
        $array = 1;
        $title = 'Array Output';
    } else {
        $array = 0;
        $title = 'Variable Output';
    }
    
    echo '<div style="background:#eee; border:1px solid #888; color: #444; font-size: 12px; clear:both;">'."\n";
    echo '<h5 style="font-weight:bold; font-size:1.2em; line-height: 1.4em;">'.$title.'</h5>'."\n";
    if($array) {
        echo '<pre>'."\n";
        print_r($var);
        echo '</pre>'."\n";
    } else {
        echo $var;
    }
    echo '</div>'."\n";
}
#17

[eluser]xwero[/eluser]
more hints :

- Use config files for most of your configurable libraries. Then values only have to be changed in one place.

config
upload.php
image_lib.php

- When you extend a controller or a model use include to import the baseclass in the children classes
Code:
// basecontroller.php
class Basecontroller extends Controller
{
   function Basecontroller()
   {
      parent::Controller();
   }
   // other methods
}
// childcontroller.php
include('basecontroller.php');
class Childcontroller extends Basecontroller
{
   function Childcontroller()
   {
      parent::Basecontroller();
   }
   // other methods
}
#18

[eluser]Référencement Google[/eluser]
[quote author="xwero" date="1203003692"]
- When you extend a controller or a model use include to import the baseclass in the children classes
Code:
// basecontroller.php
class Basecontroller extends Controller
{
   ....
[/quote]

You won't have to use include if you use a MY_Controller.php in your library folder as Basecontroller. So one hint would be to use:

Code:
// In application/library/MY_Controller.php
class MY_Controller extends Controller {

    function MY_Controller()
    {
       parent::Controller();
    }

    function yourfunction()
    {
        echo "Hello world";
    }
}

Then yourfunction() will be avaible sitewide in every other controllers that extends the MY_Controller (without having to include them) for example using:
Code:
$this->yourfunction();
#19

[eluser]xwero[/eluser]
elitemedia that is an extra tip Smile

I've seen snippets on the forum where people do this
Code:
class MY_Controller extends Controller {

    function MY_Controller()
    {
       parent::Controller();
    }
}
class Admincontroller extends MY_Controller
{

}

class Frontendcontroller extends MY_Controller
{

}
in the My_Controller file and i think it's not a behavior that needs to be encouraged. I like the one class per file rule of thumb.

You also can't extend a child class of a class that is defined in the my_controller file. Most people won't need to do this but if you have to do it it's best to have a consistent method of extending classes.
#20

[eluser]codex[/eluser]
[quote author="Lone" date="1202892803"]Yet Another Hint - Easy Array/Variable Output

From the previous CMS we built we had the following function to help us with out with viewing what is in an array or variable. It's not the prettiest looking output but saves a heap of time when developing.

We added it to CodeIgniter by making it a helper called 'debug_helper.php' and just autoload it.

Example
Code:
$people = array('John','Mary','Alberto');
p($people); // Will print_r onto screen with a <pre> tag

Helper Code
Code:
function p($var)
{
    if(is_array($var)) {
        $array = 1;
        $title = 'Array Output';
    } else {
        $array = 0;
        $title = 'Variable Output';
    }
    
    echo '<div style="background:#eee; border:1px solid #888; color: #444; font-size: 12px; clear:both;">'."\n";
    echo '<h5 style="font-weight:bold; font-size:1.2em; line-height: 1.4em;">'.$title.'</h5>'."\n";
    if($array) {
        echo '<pre>'."\n";
        print_r($var);
        echo '</pre>'."\n";
    } else {
        echo $var;
    }
    echo '</div>'."\n";
}
[/quote]

Actually that's not a bad idea at all. I'm going to copy this. Thanks! ;-)




Theme © iAndrew 2016 - Forum software by © MyBB