Welcome Guest, Not a member yet? Register   Sign In
New with Code Igniter - Application structure
#11

[eluser]bisol[/eluser]
I've beginning my website.

Tell me if I'm wrong.

Edit
I think I'm wrong because on each page, I load the column2.php and column3.php (view including view) but I need to write the SQL into each part of the main controller.

example.

function : Home
- Load top10 web site
- load home

function : Contact
- Load top10 web site
- load contact

....
End edit

Controller
Code:
class Accueil extends Controller {

    //Fonction de construction
    function __construct()
    {
        parent::Controller();
        
    }

    //Page affichée si aucun paramètre n'est envoyé
    function index()
    {
        //Header
        $data['header_title'] ='Annuaire généraliste gratuit beegoo';
        $data['header_keyword'] = 'beegoo, annuaire, annuaire generaliste, internet';
        $data['header_description'] = 'Annuaire beegoo, annuaire généraliste gratuit avec lien en dur et sans lien de retour';
        $data['header_css'] = '/default.css';
        
        //Loading data sql
        $data['test'] = $this->db->version();
        
        
        
        $this->load->view('accueil', $data);
    }

    function contact()
    {
        //Loading data sql
        $data['test'] = $this->db->version(); /// THIS PART IS THE SAME THAT THE ONE IN "index"
      
       $data['title'] = "test"
  
    }
}

View (accueil)

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$this->load->view("header");
//$this->load->view("");
$this->load->view("footer");
?>

View (header)
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
<!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;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;meta name="keywords" content="&lt;?=$header_keyword?&gt;" /&gt;
&lt;meta name="description" content="&lt;?=$header_description?&gt;" /&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?=$header_css?&gt;" /&gt;
&lt;title&gt;&lt;?=$header_title?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
#12

[eluser]Fero[/eluser]
$data['test'] = $this->db->version();

what the heck is this???
#13

[eluser]bisol[/eluser]
This is only an example to describe my problem. This code is a code needed in each part of the main page (top10, home, ....).

Now, I will create a model file and called it like this : $data[‘top_10’] = $this->MainModel->get_top_10();
#14

[eluser]ayukawaa[/eluser]
This is well documented in the forums and it has been answered many times I think.

For me, instead of creating many controllers that extend the 'Controller' (repeating code in all the controllers), I create one MY_Controller library which extends Controller and I put in there all the common code.

Then, all the controllers must extend My_Controller, NOT Controller

For example, create your controllers like
Code:
class ControllerExample extends MY_Controller {
    
    function ControllerExample()
    {
        //this call the MY_CONTROLLER (parent) constructor
        parent::MY_Controller();
    }

    function index()
    {
        //generate one $config array with all the parts that are UNIQUE in this controller
        $data['var1'] = ...
        $data['var2'] = ...
        $data['var3'] = ...
        $config['main_content'] = $this->load->view('main_content', $data, true);
        $config['section1'] = $this->load->view('section1', $data, true);
        $config['section2'] = $this->load->view('section2', $data, true);
        $config['section3'] = $this->load->view('section3', $data, true);
        (...)    
        //End, screen output
        $this->output_screen( $config );
    }

    (...)
    
}
And create in application/libraries/MY_Controller.php:
Code:
class MY_Controller extends Controller{
    
    function MY_Controller()
    {
        //this call the parent constructor
        parent::Controller();
    }

    //ONLY ONE output point, pass all data through array
    function output_screen($config){
        //append to the $config array all the common data you need (menus, etc)
        $data['var1'] = ...
        $data['var2'] = ...
        $data['var3'] = ...
        $config['commonsection1'] = $this->load->view('commonsection1', $data, true);
        $config['commonsection2'] = $this->load->view('commonsection2', $data, true);
        $config['commonsection3'] = $this->load->view('commonsection3', $data, true);
        (...)
        //SCREEN OUTPUT (this should be the only one outputting to screen)
        $this->load->view('template_of_sections', $config);
    }

    (...)
}

And that's all.

^_^
#15

[eluser]taro uma[/eluser]
does anyone have an example of how you pass variables from MY_Controller to a controller that extends it?

In this last example, I kinda see how this is loading a view that has the variables in it, but what if you just want to pass the variables without having a view? I just want to pass the $data array along and access it in the controller that extends MY_Controller, and i don't exactly understand how this is done.
#16

[eluser]jedd[/eluser]
[quote author="taro uma" date="1238813037"]does anyone have an example of how you pass variables from MY_Controller to a controller that extends it?[/quote]

How about [url="http://ellislab.com/forums/viewreply/553141/"]this one[/url]?
#17

[eluser]taro uma[/eluser]
I've seen that and it helped some, I was able to create the MY_Controller based on that info. But I still can't see how to get the variables from MY_Controller to the controller that is extending it.

Here is what I have so far, I just can't get the $data from MY_Controller to pass along.

Code:
class MY_Controller extends Controller {

    function MY_Controller()
    {
        // load controller parent
        parent::Controller();

        $this->data['sitewide']['mainNav'] = $this->getMainNav();
        $this->data['sitewide']['sideBar'] = $this->getSideBar();
        $this->data['sitewide']['footer'] = $this->getFooter();
    }

    private function getMainNav()
    {
        $mainNav = "asdfg1";
        return $mainNav;
    }


    private function getSideBar()
    {
        $sideBar= "asdfg2";
        return $sideBar;
    }


    private function getFooter()
    {
        $footer= "asdfg3";
        return $footer;
    }
}

but it's not giving me those variables in a control that extends it:

Code:
class FrontPage extends MY_Controller {

    function __construct(){
      parent::MY_Controller();
    }

    function index()
    {
        $this->load->model('FrontPageModel','',TRUE);
        $data['frontpagedata'] = $this->FrontPageModel->get_frontpage_content();
        $this->load->view('frontpage',$data);
    }
}

This passes $data along to my view, which just echos out the $data variables (right now).
I am expecting $sitewide to be an array that exists with my site wide stuff in it. But it doesn't, so I know I'm doing it wrong. the load->view is passing $data along, because in the view I have the $frontpagedata, which is an array of that builds the content part of the page. But trying to use $sitewide , like echo $sitewide['mainNav') gives me as error "Message: Undefined variable: sitewide"

So I see where I'm doing it differently as far as not loading a view in MY_Controller, from the example you show. But I can't see why the variables aren't in the $data array.

In my view, I have this:

Code:
&lt;?php echo $sitewide['mainNav']; ?&gt;

and it returns this error message:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: sitewide

Filename: views/frontpage.php

Line Number: 34
#18

[eluser]jedd[/eluser]
In your (normal) controller, change this line:

Code:
$this->load->view('frontpage',$data);

to this:
Code:
$this->load->view('frontpage',$this->data);

... and give it another try.
#19

[eluser]taro uma[/eluser]
Ah. That works. So I can see now that $this->data and $data are not the same.

But I don't understand why in MY_Controller, I can't just say
Code:
$data['sitewide']['mainNav'] = $this->getMainNav();

instead of
Code:
$this->data['sitewide']['mainNav'] = $this->getMainNav();

what is the $this-> part doing to make the variables available to the normal controller?
#20

[eluser]TheFuzzy0ne[/eluser]
It's a question of scope. $data is available within the confines of the function it's declared in, if you declare $data as a class variable, it's available to the whole class, and accessed via $this->data. $this->something means you want to access a variable that's in the global scope of the current object. $this won't work outside of an object.

Code:
class Example {

    var $data = 'Class data';

    function getData($from_class=FALSE)
    {
        $data = "Function data";        

        if ($from_class === TRUE)
        {
            return $this->data; # returns the class variable
        }
        else
        {
            return $data; # returns the function variable.
        }
    }
}

$example = new Example();

echo $example->getData() . "\n"; # Echoes "Function data"
echo $example->getData(TRUE) . "\n"; # Echoes "Class data"
The above code is untested.

I hope this helps without causing more confusion.




Theme © iAndrew 2016 - Forum software by © MyBB