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).


Messages In This Thread
CodeIgniter helpful hints - by El Forum - 02-07-2008, 08:03 PM
CodeIgniter helpful hints - by El Forum - 02-07-2008, 10:57 PM
CodeIgniter helpful hints - by El Forum - 02-07-2008, 11:30 PM
CodeIgniter helpful hints - by El Forum - 02-08-2008, 12:32 AM
CodeIgniter helpful hints - by El Forum - 02-08-2008, 06:24 AM
CodeIgniter helpful hints - by El Forum - 02-08-2008, 07:17 AM
CodeIgniter helpful hints - by El Forum - 02-08-2008, 07:37 AM
CodeIgniter helpful hints - by El Forum - 02-08-2008, 10:36 PM
CodeIgniter helpful hints - by El Forum - 02-08-2008, 10:52 PM
CodeIgniter helpful hints - by El Forum - 02-09-2008, 12:14 AM
CodeIgniter helpful hints - by El Forum - 02-09-2008, 01:57 AM
CodeIgniter helpful hints - by El Forum - 02-12-2008, 02:25 AM
CodeIgniter helpful hints - by El Forum - 02-12-2008, 03:01 AM
CodeIgniter helpful hints - by El Forum - 02-12-2008, 11:29 AM
CodeIgniter helpful hints - by El Forum - 02-12-2008, 04:30 PM
CodeIgniter helpful hints - by El Forum - 02-12-2008, 08:53 PM
CodeIgniter helpful hints - by El Forum - 02-14-2008, 03:41 AM
CodeIgniter helpful hints - by El Forum - 02-14-2008, 06:46 AM
CodeIgniter helpful hints - by El Forum - 02-14-2008, 07:47 AM
CodeIgniter helpful hints - by El Forum - 02-14-2008, 09:52 AM
CodeIgniter helpful hints - by El Forum - 02-19-2008, 08:47 AM
CodeIgniter helpful hints - by El Forum - 02-19-2008, 09:38 AM
CodeIgniter helpful hints - by El Forum - 02-19-2008, 09:41 AM
CodeIgniter helpful hints - by El Forum - 02-19-2008, 06:43 PM
CodeIgniter helpful hints - by El Forum - 02-19-2008, 10:15 PM
CodeIgniter helpful hints - by El Forum - 02-20-2008, 01:35 AM
CodeIgniter helpful hints - by El Forum - 02-20-2008, 02:06 AM
CodeIgniter helpful hints - by El Forum - 02-20-2008, 03:00 AM
CodeIgniter helpful hints - by El Forum - 02-26-2008, 08:13 AM
CodeIgniter helpful hints - by El Forum - 03-06-2008, 04:38 AM
CodeIgniter helpful hints - by El Forum - 03-06-2008, 08:32 AM
CodeIgniter helpful hints - by El Forum - 04-03-2008, 10:19 AM
CodeIgniter helpful hints - by El Forum - 04-04-2008, 10:07 PM
CodeIgniter helpful hints - by El Forum - 04-11-2008, 03:13 AM
CodeIgniter helpful hints - by El Forum - 04-18-2008, 06:35 AM
CodeIgniter helpful hints - by El Forum - 04-28-2008, 10:34 PM
CodeIgniter helpful hints - by El Forum - 05-14-2008, 01:23 AM
CodeIgniter helpful hints - by El Forum - 05-15-2008, 05:48 AM
CodeIgniter helpful hints - by El Forum - 06-18-2008, 02:40 AM
CodeIgniter helpful hints - by El Forum - 07-09-2008, 05:03 AM
CodeIgniter helpful hints - by El Forum - 07-09-2008, 05:05 AM
CodeIgniter helpful hints - by El Forum - 07-30-2008, 04:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB