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

[eluser]Michael Ekoka[/eluser]
@xwero about reply #18: I tend to disagree as I think absolutely the opposite.

The fact of the matter is that you need Base controllers, otherwise, it implies that your code is very repetitive. Some controllers share the same behavior, especially at initialization, it is perfectly natural to group these behaviors in a parent class. This is exactly what inheritance is about. The MY_Controller.php file is a convenience file provided to do exactly that. You don't have to name your extended controller MY_Controller for it to be available. I guess people do it for consistency, but you can as well name it 'Base' and create other Parent classes in the same file with other arbitrary names. They still will be available to your application's controllers.

The 'One-class-per-file' rule of thumb applies to concrete controllers (created in /controllers). Think of the parents as pseudo abstract classes that simply help their child with common functionalities (initialization, authentication, etc). They have no power on their own. This absolutely and in no way breaks the MVC pattern, on the contrary.

It is true that you need a consistent method of extending controller classes and the down point of this method is that if you create too many base controllers you end up with a bloated file. Unfortunately, CI does not provide another standardized way of doing this. The other solution is to do what everybody was doing when each one of us had our very own MVC framework and which is still legit and very kosher. create a base_controllers folder and in it you can create all the base controllers that you need each in its very own file if you want. They can extend the Controller class or another base controller. Then at the top of each of your application's controller, include the required base controller's file with a 'require' or 'include' call.

Now for my tip:
Many people don't know this, but the php closing tag '?&gt;' is not required. It is perfectly ok not to put it at the end of your php file if you do not intertwine HTML and PHP content in it. By HTML, I mean real HTML where you close the php tag (?&gtWink, output some contents and then reopen the tag (&lt;?php). If it's 'echoed' HTML (echo '<p>my paragraph</p>'Winkyou can still omit the closing tag. I'd therefore encourage you to close your php processes in all your view files with '?&gt;', and omit that closing tag in the rest of the application. This may save you the headache caused by carelessly inserted empty spaces and line feeds after it in certain files (the error message usually says something about headers already been sent).
#22

[eluser]xwero[/eluser]
[quote author="CodeIgniter" date="1203454058"]@xwero about reply #18: I tend to disagree as I think absolutely the opposite.

The fact of the matter is that you need Base controllers, otherwise, it implies that your code is very repetitive. Some controllers share the same behavior, especially at initialization, it is perfectly natural to group these behaviors in a parent class. This is exactly what inheritance is about. The MY_Controller.php file is a convenience file provided to do exactly that. You don't have to name your extended controller MY_Controller for it to be available. I guess people do it for consistency, but you can as well name it 'Base' and create other Parent classes in the same file with other arbitrary names. They still will be available to your application's controllers.
[/quote]
I'm not saying you don't need base controllers. I'm saying break up your files as much as possible. When there are changes later you can limit the impact of the changes to the files that need the change. For instance:
Code:
// my_controller.php
My_controller extends Controller{}
// admin_controller.php
include('my_controller.php');
Admin_controller extends MY_Controller{}
// users.php
include('admin_controller.php');
Users extends Admin_Controller{}
// blog.php
include('admin_controller.php');
Blog extends Admin_Controller{}
// rss.php
include('admin_controller.php');
Rss extends Admin_Controller{}
You need to make changes to the admin_controller that only affect the blog and rss child classes. You only have to create another file and change the include for those child classes. If there is an error in the code it only affects those child classes.

If you put the new my_controller extended class in the my_controller file all children will be affected if there is an error.
#23

[eluser]Lone[/eluser]
@codex: Good to see it came handy - we use it heaps!

And even another tip
Just started using this one in the past couple of days as we love the info you can get from the standard Profiler class. Problem is we don't like adding/removing code each time we want/dont want to see it.

Somewhere in your config/config.php file place the following:
Code:
$config['profiler'] = 1;

Then in each of your controllers in the '__construct' function (add it if you dont have it already)
Code:
if($this->config->item('profiler') {
  $this->output->enable_profiler(TRUE);
}

Now in your config.php file you can switch on/off the profiler for debugging whenever you want Smile
#24

[eluser]Référencement Google[/eluser]
One more tip that I use. I define in my config.php a constant that says if I am on my production server or on my localhost. It allow me then to reach different settings that are switched automatically, example with the base URL:

Code:
define('IS_PRODUCTION', ($_SERVER['SERVER_NAME'] != 'localhost'));
                      
if(IS_PRODUCTION)
{
    $config['base_url'] = 'http://www.yoursite.com';
}
else
{
    $config['base_url'] = 'http://localhost/yoursite/';
}

Or for example in the database.php file:
Code:
if(IS_PRODUCTION)
{
    $active_group = 'production';
}
else
{
    $active_group = 'local';
}
                
$active_record = TRUE;

$db['local']['hostname'] = "localhost";
$db['local']['username'] = "root";
$db['local']['password'] = "pass";
$db['local']['database'] = "dbname";
$db['local']['dbdriver'] = "mysql";
$db['local']['dbprefix'] = "";
$db['local']['pconnect'] = FALSE;
$db['local']['db_debug'] = TRUE;
$db['local']['cache_on'] = FALSE;
$db['local']['cachedir'] = "";
$db['local']['char_set'] = "utf8";
$db['local']['dbcollat'] = "utf8_general_ci";

$db['production']['hostname'] = "localhost";
$db['production']['username'] = "production_user";
$db['production']['password'] = "production_pass";
$db['production']['database'] = "production_db";
$db['production']['dbdriver'] = "mysql";
$db['production']['dbprefix'] = "";
$db['production']['pconnect'] = FALSE;
$db['production']['db_debug'] = FALSE;
$db['production']['cache_on'] = FALSE;
$db['production']['cachedir'] = "";
$db['production']['char_set'] = "utf8";
$db['production']['dbcollat'] = "utf8_general_ci";
#25

[eluser]John_Betong[/eluser]
&nbsp;
&nbsp;
I find this &nbsp; _menu.php &nbsp; invaluable.

More details can be found here &nbsp; http://ellislab.com/forums/viewthread/59270/
&nbsp;
&nbsp;
#26

[eluser]Référencement Google[/eluser]
[quote author="John_Betong" date="1203502549"]&nbsp;
&nbsp;
I find this &nbsp; _menu.php &nbsp; invaluable.

More details can be found here &nbsp; http://ellislab.com/forums/viewthread/59270/
&nbsp;
&nbsp;[/quote]

I don't get it, what's the point on that?
#27

[eluser]xwero[/eluser]
One more hint (inspired by this thread):

Instead of hardcoding the fieldnames in the callback function, make use of the parameter to identify the fields

Code:
$rules['field1'] = 'callback_rule[field2,field3]';

function rule($str,$param)
{
    list($post1,$post2) = explode(',',$param);
    $post1val = $this->input->post($post1);
    $post2val = $this->input->post($post2);
    if(!$post1val OR !$post2val)
    {
       return FALSE;
    }

    if(strcasecmp($str,$post1val) < 0 OR strcasecmp($str,$post1val) < 0)
    {
       return FALSE;
    }

    return TRUE
}

If the fieldnames change you can change them in the rule instead of in the callback function and if you use the function for multiple sites/apps you can add it to an extended Validation library (MY_Validation).
#28

[eluser]John_Betong[/eluser]
[quote author="elitemedia" date="1203514527"][quote author="John_Betong" date="1203502549"]&nbsp;
&nbsp;
I find this &nbsp; _menu.php &nbsp; invaluable.

More details can be found here &nbsp; http://ellislab.com/forums/viewthread/59270/
&nbsp;
&nbsp;[/quote]

I don't get it, what's the point on that?[/quote]
&nbsp;
Did you read the post?

Here is a snippet:
Quote:Preamble

Delphi has a wonderful technique that allows the programmer to easily switch
between applications by using a menu system.

CodeIgniter’s rigidity and hard-coding has been bothering me for quite some time
as to the easiest way to achieve:
1. easily switch CodeIgniter applications
2. use a common CodeIgniter System folder.

&nbsp;
&nbsp;
#29

[eluser]xwero[/eluser]
the hint of the day Smile

use the same name as the view file for the vars array that is added to the view
Code:
$header['title'] = 'some title';
$this->load->view('header',$header);
$content['title'] = 'some title';
$this->load->view('content',$content);
$footer['copyright'] = '2008';
$this->load->view('footer',$footer);
Simple but effective.
#30

[eluser]xwero[/eluser]
keeping the thread alive with one more hint

Keep the loading of libraries out of loops because from the second run the library will give the message it's already loaded and won't execute. I'm thinking about the upload and image_lib libraries




Theme © iAndrew 2016 - Forum software by © MyBB