Welcome Guest, Not a member yet? Register   Sign In
What is best structure for a big application?
#5

[eluser]Edemilson Lima[/eluser]
Thank you for the answer! Your template class is nice, but I don't know if it is exactly what I need. I will try to demonstrate my problem with some examples.

In the old days, I was used to load everything using include() or require(), like this:

index.php:
Code:
<?php
require('mylibrary/index.php'); // starts session, validades user, load helpers, etc.
if(!$section) $section='Home';
?>
<html>
<head>
<title>Web Site Title - <?=$section?></title>
<style rel="stylesheet" href="layout/mystyles.css" type="text/css" />
</head>
<body>
<div id="header">&lt;?php require('header.php'); ?&gt;</div>
<div id="lateral">&lt;?php
if(!$lateral) $lateral='categories';
require('lateral.php');
?&gt;</div>
<div id="contents">&lt;?php
if(!$contents) $contents='home.php';
require("{$contents}.php");
?&gt;</div>
<div id="footer">&lt;?php require('footer.php'); ?&gt;</div>
&lt;/body&gt;
&lt;/html&gt;

header.php:
Code:
<img src="layout/logomark.gif"><br />
&lt;?php require('menu.php'); ?&gt;

lateral.php:
Code:
blah blah blah<br />
&lt;?php require("{$lateral}.php"); ?&gt;

categories.php:
Code:
&lt;?php
// code to load a list of categories from a database and display it
?&gt;

home.php:
Code:
<div id="news">&lt;?php require('news.php'); ?&gt;</div>
<div id="offers">&lt;?php require('offers.php'); ?&gt;</div>

news.php:
Code:
&lt;?php
// code to load news from a database or RSS and display them
?&gt;

offers.php:
Code:
&lt;?php
// code to load offers from a database and display them
?&gt;

footer.php:
Code:
<div id="footer">Credits, some links, etc</div>

products.php:
Code:
&lt;?php
if($action=='add') {
  // code to include a new product
}
if($action=='update') {
  // code to update a product
}
if($action=='remove') {
  // code to remove a product from the database
}
if(!$action or $action=='list') {
  // code to show the product list
}
?&gt;

As you can see, when index.php is executed, it loads the blocks, which loads their sub blocks and so on. Everything is done automatically, as one is inside another. I don't need to rebuild every block at every request.

If I pass to the variable $content the value 'products', the index.php will include the 'products.php' instead of 'home.php'. In the case of product.php, I can choose which process will be executed, changing the $action variable in some way. I know, this is pure procedural, but it works.


Well, lets do it in the CodeIgniter way:

welcome.php:
Code:
&lt;?php
class Welcome extends Controller {

    function Welcome() {
        parent::Controller();
        $this->load->model('categories');
        $this->load->model('news');
        $this->load->model('offers');
    }

    function index() {
        $header['menu']=$this->load->view('menu_view','',true);

        $categories=$this->categories->list();
        $lateral['lateral']=$this->load->view('categories_view',$categories,true);

        $news=$this->model->news->list();
        $home['news']=$this->load->view('home_view',$news,true);

        $offers=$this->model->offers->list();
        $home['offers']=$this->load->view('offers_view',$offers,true);

        $data['section']='Home';
        $data['header']=$this->load->view('header_view',$header,true);
        $data['lateral']=$this->load->view('lateral_view',$lateral,true);
        $data['contents']=$this->load->view('home_view',$home,true);
        $data['footer']=$this->load->view('footer_view','',true);
        $this->load->view('main_view',$data);
    }
}
?&gt;

main_view:
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Web Site Title - &lt;?=$section?&gt;&lt;/title&gt;
&lt;style rel="stylesheet" href="layout/mystyles.css" type="text/css" /&gt;
&lt;/head&gt;
&lt;body&gt;
<div id="header">&lt;?=$header?&gt;</div>
<div id="lateral">&lt;?=$lateral?&gt;</div>
<div id="contents">&lt;?=$contents?&gt;</div>
<div id="footer">&lt;?=$footer?&gt;</div>
&lt;/body&gt;
&lt;/html&gt;

header_view:
Code:
<img src="layout/logomark.gif"><br />
&lt;?=$menu?&gt;

lateral_view:
Code:
blah blah blah<br />
&lt;?=$lateral?&gt;

home_view:
Code:
<div id="news">&lt;?=$news?&gt;</div>
<div id="offers">&lt;?=$offers?&gt;</div>

Well, I think is not necessary to show news_view, offers_view or footer_view here. Please continue reading the next post.


Messages In This Thread
What is best structure for a big application? - by El Forum - 01-14-2008, 07:09 AM
What is best structure for a big application? - by El Forum - 01-14-2008, 07:24 AM
What is best structure for a big application? - by El Forum - 01-14-2008, 09:45 AM
What is best structure for a big application? - by El Forum - 01-17-2008, 12:43 AM
What is best structure for a big application? - by El Forum - 01-17-2008, 06:33 AM
What is best structure for a big application? - by El Forum - 01-17-2008, 06:34 AM
What is best structure for a big application? - by El Forum - 01-17-2008, 07:06 AM
What is best structure for a big application? - by El Forum - 01-17-2008, 11:55 AM
What is best structure for a big application? - by El Forum - 01-17-2008, 02:14 PM
What is best structure for a big application? - by El Forum - 01-17-2008, 02:15 PM
What is best structure for a big application? - by El Forum - 01-17-2008, 03:20 PM
What is best structure for a big application? - by El Forum - 01-17-2008, 03:27 PM
What is best structure for a big application? - by El Forum - 01-17-2008, 06:55 PM
What is best structure for a big application? - by El Forum - 01-17-2008, 07:14 PM
What is best structure for a big application? - by El Forum - 01-19-2008, 08:20 PM
What is best structure for a big application? - by El Forum - 01-20-2008, 10:40 AM
What is best structure for a big application? - by El Forum - 01-21-2008, 11:19 AM
What is best structure for a big application? - by El Forum - 02-02-2008, 01:35 PM
What is best structure for a big application? - by El Forum - 02-02-2008, 03:53 PM
What is best structure for a big application? - by El Forum - 02-02-2008, 05:15 PM
What is best structure for a big application? - by El Forum - 02-02-2008, 05:51 PM
What is best structure for a big application? - by El Forum - 02-03-2008, 06:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB