Begining CI / Frameworks in General |
[eluser]Aea[/eluser]
I'm a fairly new PHP coder who began learning PHP to use on personal sites, I had a CMS on my website so I had a "framework" already for me to develop on. Right now I'm building a few websites from the ground up and decided to use a framework to make it simple. I never used OOP programming before (somebody point me to a good tutorial that explains why I need it, and not how I'm supposed to use it), but I'm pretty open-minded and quite willing to make this work. I narrowed my experimenting down to ZF & CI, after getting confused on the reasoning behind controllers in ZF, I tried CI which explained not only how they work, but why the front-loader was even necessary. I must say I'm a bit worried since even CI seems quite complex in the settings it offers (but ultimately I'm sure I'll understand this shortly). I'm currently at the very first part of the user guide, General Topics -> Controllers. I can't get the blog.php controller working, and the error I'm having is fairly strange. I just get an empty 36B HTML page sent. I tried accessing the welcome message (worked fine), and I managed to get the blog.php controller somewhat working by putting... Code: $this->load->view('welcome_message'); ... into the index function. This is more of a workaround to confirm that the basic framework is working, but I'm still curious as to why it won't perform my echo statement as seen in the guide. Incidentally I tried adding... Code: function Blog() ... but this seems redundant and doesn't really do anything. Any help would be appreciated :blank:
[eluser]awpti[/eluser]
Howdy. I have the same type of system.. Here's how it works for me: /controlers/blog.php Code: class Blog extends Controller { And.. /views/blog/front.php Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" Hope this blob of code helps. If you're looking for a "WHY" explanation, check out the 20 minute long blog video tutorial. It's explained fairly well.
[eluser]esra[/eluser]
The Blog function is your constructor for PHP4 only. You can use a PHP5 style constuctor function when using PHP5. The constructor is particularly useful if you write a base controller and create new controllers by extending the base controller. You can add code to the constructor that you want the child controllers to globally inheirit. For example, a base controller... Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Now a sibling controller based on the Application Controller... Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); Another sibling controller... Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); With OOP, you want to create parent classes which have methods to pass on to sibling classes. This minimizes the amount of code you need to write in your sibling controllers. In the above, the Blog and Articles controllers would inheirit the operations included in the Application controllers constructor. For example, Articles and Blog would be able to access the methods (functions) in the Btemplate and Breadcrumb library, loaded by the parent Application controller. After CI processes the constructor code, the Index function is processed if one exists. This allows you to execute controller-specific construction code. That is, the code in the Index function of your Application controller would be overwritten by whatever code existed in the Index function of the Blog and Articles controllers. There are some nice tutorials on the web about object oriented programming with PHP. Some include comparisons that show how procedural code is rewritten to take advantage of OOP. You might do some searches for these. For some additional searches, try 'php design patterns'. Design patterns allow you to further modularize your code into objects and minimize code redundancy.
[eluser]Derek Allard[/eluser]
esra... sweet post. Well done, thanks. Aea, you might want to take a look at ExpressionEngine. It is the content management software that CI was created from, and also, it drives the CI development to a considerable degree. Imagine a CMS built on CI, and that's what you have. Its free for non-profit uses also. That said, if you have the time and/or opportunity, it may well be worth it to stick out the CI learning curve. It'll make you a better programmer, I promise you that Welcome aboard!
[eluser]Aea[/eluser]
[quote author="Derek Allard" date="1183611691"]esra... sweet post. Well done, thanks. Aea, you might want to take a look at ExpressionEngine. It is the content management software that CI was created from, and also, it drives the CI development to a considerable degree. Imagine a CMS built on CI, and that's what you have. Its free for non-profit uses also. That said, if you have the time and/or opportunity, it may well be worth it to stick out the CI learning curve. It'll make you a better programmer, I promise you that Welcome aboard![/quote] Thank you for the Expression Engine recommendation, the software just isn't for me as there's a cost attached for commercial development which I just can't justify over developing from scratch, especially since I like to have neat (even if it was procedural so far) code
|
Welcome Guest, Not a member yet? Register Sign In |