Welcome Guest, Not a member yet? Register   Sign In
Begining CI / Frameworks in General
#1

[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()
    {
        parent::Controller();    
    }

... but this seems redundant and doesn't really do anything.

Any help would be appreciated :blank:
#2

[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 {

    private $awTitle = 'awpti.org - Home of geeking, PHP and SQL!';
    private $awHeader = 'awpti.org';
    private $awNewsLimit = 3;
    
    function Blog() {
        parent::Controller();
    }

    function index() {
        $data['Title'] = $this->awTitle;
        $data['Header'] = $this->awHeader;

        $query_news = 'SELECT * FROM awNews ORDER BY NewsID DESC LIMIT '.$this->awNewsLimit;
        $data['Query_News'] = $this->db->query($query_news);

        $this->load->view('blog/front', $data);
    }

    function about() {
        $data['Title'] = $this->awTitle.' (About)';
        $data['Header'] = $this->awHeader;

        $this->load->view('blog/about', $data);
    }

} //end class. Duh.

And.. /views/blog/front.php

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;&lt;?= $Title; ?&gt;&lt;/title&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
    &lt;meta http-equiv="Content-Language" content="en-us" /&gt;
    &lt;meta http-equiv="imagetoolbar" content="no" /&gt;
    &lt;meta name="MSSmartTagsPreventParsing" content="true" /&gt;
    &lt;meta name="keywords" content="awpti, php, sql, geeking" /&gt;
    &lt;meta name="author" content="Geoff Winans (http://www.awpti.org/)" /&gt;
    &lt;style type="text/css" media="all"&gt;@import "/media/style/style.css";&lt;/style&gt;
    &lt;link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="rss/" /&gt;
&lt;/head&gt;

&lt;body&gt;


<div class="content">
    
    <div class="toph"></div>
    <div style="display: none;">&lt;?= $Header; ?&gt;</div>
    <div class="right">
        <div class="title">AWPTI.ORG</div>
        <div class="nav">
        <ul>
            <li><a href="/">HOME</a></li>
            <li><a href="/about/">ABOUT</a></li>
            <li><a href="http://gallery.awpti.org/">GALLERY</a></li>
        </ul>
        </div>

        <h2>Top Links:</h2>
         <ul>
            <li><a href="http://www.galerion.org">galerion.org</a></li>
            <li><a href="http://www.solucija.com">Solucija</a></li>
            <li><a href="http://www.newzbin.com">NewzBin</a></li>
            <li><a href="http://www.google.com/reader/">gReader</a></li>
            <li><a href="http://www.google.com/mail/">gMail</a></li>
         </ul>
        <hr />
        &lt;!--
        <h2>Search</h2>
        &lt;form action="#"&gt;
            &lt;input type="text" value="" class="text" /&gt;
            &lt;input type="submit" value="Go" class="searchbutton" /&gt;
        &lt;/form&gt;
        --&gt;
        <br /><br />
        <hr />
    
        <div class="footer_text">
        &copy; Copyright awpti.org<br />
        Design by: <a href="http://www.free-css-templates.com/" title="Free CSS Templates">David Herreman</a><br />
        &lt;!-- <a href="rss/">RSS Feed</a> | --&gt;<a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> and <a href="http://validator.w3.org/check?uri=referer">XHTML</a> &lt;!-- | <a href="#">Login</a> --&gt;
        </div>
        
    </div>
    
    <div class="center">
&lt;?php
    foreach($Query_News->result() as $news) {
        echo '<h2>',$news->PostTitle,"</h2>\n";
        echo $news->Content."\n";
        echo '<p class="date">Posted by ',$news->PostBy,' <img src="/media/images/comment.gif" alt="comments" /> <a href="#">Comments (##)</a> <img src="/media/images/timeicon.gif" alt="" /> ',unix_to_human(mysql_to_unix($news->PostTime))."</p><br />\n";
    }
?&gt;
&lt;!--
        <div class="boxad">
            Box for ads or whatever. //Unused.
        </div>
--&gt;

    </div>
    
    
    <div class="footer"> </div>
</div>

&lt;/body&gt;
&lt;/html&gt;

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.
#3

[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:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Application extends Controller
{
    function Application()
    {
        parent::Controller();
        $this->load->library('Btemplate');
        $this->load->library('Breadcrumb');
        $this->breadcrumb->set_delimiter('&raquo;');
        
    }

    function Index()
    {
         // controller-specific construction code

    }
}
?&gt;

Now a sibling controller based on the Application Controller...

Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends Application
{
    function Blog()
    {
        parent:Application();

    }

    function Index()
    {
        // controller-specific constructor code

    }
}

Another sibling controller...

Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Articles extends Application
{
    function Articles()
    {
        parent:Application();

    }

    function Index()
    {
        // controller-specific constructor code

    }
}

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.
#4

[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 Wink

Welcome aboard!
#5

[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 Wink

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 Smile
#6

[eluser]Derek Allard[/eluser]
You're welcome, and of course I understand.




Theme © iAndrew 2016 - Forum software by © MyBB