CodeIgniter helpful hints |
[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 '?>' 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 (?>, output some contents and then reopen the tag (<?php). If it's 'echoed' HTML (echo '<p>my paragraph</p>'you can still omit the closing tag. I'd therefore encourage you to close your php processes in all your view files with '?>', 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).
[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 If you put the new my_controller extended class in the my_controller file all children will be affected if there is an error.
[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') { Now in your config.php file you can switch on/off the profiler for debugging whenever you want
[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')); Or for example in the database.php file: Code: if(IS_PRODUCTION)
[eluser]John_Betong[/eluser]
I find this _menu.php invaluable. More details can be found here http://ellislab.com/forums/viewthread/59270/
[eluser]Référencement Google[/eluser]
[quote author="John_Betong" date="1203502549"] I find this _menu.php invaluable. More details can be found here http://ellislab.com/forums/viewthread/59270/ [/quote] I don't get it, what's the point on that?
[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]'; 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).
[eluser]John_Betong[/eluser]
[quote author="elitemedia" date="1203514527"][quote author="John_Betong" date="1203502549"] I find this _menu.php invaluable. More details can be found here http://ellislab.com/forums/viewthread/59270/ [/quote] I don't get it, what's the point on that?[/quote] Did you read the post? Here is a snippet: Quote:Preamble
[eluser]xwero[/eluser]
the hint of the day use the same name as the view file for the vars array that is added to the view Code: $header['title'] = 'some title';
[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 |
Welcome Guest, Not a member yet? Register Sign In |