CodeIgniter Forums
Difficulty understanding how to code up a webpage - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Difficulty understanding how to code up a webpage (/thread-2492.html)



Difficulty understanding how to code up a webpage - El Forum - 08-08-2007

[eluser]Carlton[/eluser]
Hi People,

I have been looking through the code igniter documentation for about 2 weeks now and started writing some code but having difficulty grasping how it is you should create a reusable structure for a web page.

Previously I would have created nav bars, side navs in seperate php files and used the include function to place them in certain places of a template. If I were to do something similar in Code Igniter would I need to use multiple views...e.g....
Code:
$this->load->view('header_view', $data);
$this->load->view('nav_view', $data);
$this->load->view('content_view', $data);
and the data variable may contain page specific elements...such as a title...is this approach correct?

Also, how can I avoid calling 3 views on every page...would I need some sort of web page class that each page (e.g. login, search, news) extends and can simply call some sort of method that renders my main structure (i.e. create header, nav and body...then if I need to alter this I can just do it in one place)

Hopefully this question makes sense, I think I will look into buying a book which covers OO web page development or something as I think I am missing something Smile

C


Difficulty understanding how to code up a webpage - El Forum - 08-08-2007

[eluser]Michael Wales[/eluser]
Please search the forums for any of these queries:
views
partial view
view segments

There are dozens of threads posted daily on this.


Difficulty understanding how to code up a webpage - El Forum - 08-08-2007

[eluser]sophistry[/eluser]
You could also look at the FAQ for ideas.


Difficulty understanding how to code up a webpage - El Forum - 08-09-2007

[eluser]Carlton[/eluser]
Thanks for the responses, they have helped massively! :-)

I have one question though...I like the two methods outlined below especially the first...however if I have two controllers (maybe 'News' and 'Blog') is it possible to make the $sidebar variable accessible to both without the need to define a data aray for each controller? I thought by using $this->load->vars this would work allowing sidebar to be accessible to all Controllers and view...or is it only views called by the controller in which the variables were loaded?

Using the second method avoids this as it is the view requesting the data but I would prefer to pass data to the view rather than the view request it.
Code:
// METHOD 1
<html><head></head><body>
<?=header;?>
<?=sidebar;?>
<?=content;?>
<?=footer;?>
</body></html>

// METHOD 2
<html><head></head><body>
<?=$this->load->view('header');?>
<?=$this->load->view('sidebar');?>
<?=content;?>
<?=$this->load->view('footer');?>
</body></html>

// Controller e.g. Blog
function index ()
{
    $data = array(
        'page_title' => 'Blog',
        'sidebar' => $this->load->view('sidebar_view', null, true)
    );

    $this->load->vars($data);
    $this->load->view('master_view');
}



Difficulty understanding how to code up a webpage - El Forum - 08-09-2007

[eluser]sophistry[/eluser]
To get vars into multiple controllers you could:

do it the pure OOP way - extend the CI controller with a master controller that is then extended by all of your controllers, use $this->load->vars() in the master.

do it with CI hooks - load your vars via a library called by a pre controller hook.

just 2 quick ideas. maybe someone has other ways? CI has lots of ways - that's what makes it fun and challenging to find the right one for where you are at programming-skill-wise and application-complexity-wise.


Difficulty understanding how to code up a webpage - El Forum - 08-11-2007

[eluser]FX[/eluser]
[quote author="sophistry" date="1186689884"]do it the pure OOP way - extend the CI controller with a master controller that is then extended by all of your controllers, use $this->load->vars() in the master.[/quote]

I try to build my first site with CI, and i have just been trying to do that. I had my includes variables in the specific config file, that i autoload, but i would like the config items linked to my included views would be loaded once for all.

You gave me the key i miss, the $this->load->vars() method, anyway i still have a issue : extending the Controller class.

in a application/libraries/MY_Controller.php file, i have this php code :

Quote:class MY_Controller extends CI_Controller {

function MY_Controller()
{
parent::CI_Controller();
var_dump($this);
}

}

and i get a fatal error,
Quote:Fatal error: Class my_controller: Cannot inherit from undefined class ci_controller in /mnt/149/sda/5/3/radio.secteur51/system/application/libraries/MY_Controller.php on line 2

i don't understand why, i haven't changed config prefixes...


Difficulty understanding how to code up a webpage - El Forum - 08-11-2007

[eluser]esra[/eluser]
Try using Controller rather than CI_Controller for CI 1.53 or 1.54. CI normally appends CI_ to class names at several points in the code, but this does not seem to be working seemlessly in the two most recent versions, especially if your using something like the modular separation contribution.

Try this:

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

class Application extends Controller {

    function Application()
    {
        parent::Controller();
        var_dump($this);
    }

}
?>

Then using something like Coolfactor's proposed View library, you could use something like this for subclassed controllers.

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

require(APPPATH.'libraries/application'.EXT);

class I18n extends Application {

    function I18n()
    {
        parent::Application();
        $this->lang->load('i18n', 'en_us');    
    }
    
    function index()
    {
        $component = $this->lang->line('i18n_component');
        $page = $this->lang->line('i18n_title');
        
        $this->view->set('title', $page);
        $this->view->set('component', $component);
        $this->view->part('tree', 'tree');
        $this->view->part('navigation', 'navigation');
        $this->view->part('tabpanel', 'tabpanel');
        $this->view->part('content', $component);
        $this->view->part('properties', 'properties');
        $this->view->part('output', 'output');
        $this->view->load('complex');
    }
}
?>

The part code loads view fragments. It's possible to move some of these to the constructor of the base cass if you want them to always appear in your template (complex is the template or master view in this case). Or you could conditionally load view fragments by creating methods in the base controller to load different sets of view fragments (parts).


Difficulty understanding how to code up a webpage - El Forum - 08-12-2007

[eluser]FX[/eluser]
oh my... extending the core classes don't work on the current version, so ? or it's just with the Controller class ?

dealing with a master controller class was just the case i won't want to do, because it's a small site and i don't need so complex builds. Anyway, i will definitely try your advice.

for now, i've got this error :
Quote:Cannot inherit from undefined class application in /mnt/149/sda/5/3/radio.secteur51/system/application/controllers/micro.php on line 3

i've added application in my autoload libraries array, without changes...

where can i find information on controller and view classes ? i do not know the set / part methods, and i've already missed the load->vars() one...


Difficulty understanding how to code up a webpage - El Forum - 08-12-2007

[eluser]esra[/eluser]
[quote author="FX" date="1186925020"]oh my... extending the core classes don't work on the current version, so ? or it's just with the Controller class ?

dealing with a master controller class was just the case i won't want to do, because it's a small site and i don't need so complex builds. Anyway, i will definitely try your advice.

where can i find information on controller and view classes ? i do not know the set / part methods, and i've already missed the load->vars() one...[/quote]

I know it works in most versions when a controller class is extended from Controller.

Using a base controller in a simple application is optional. For OOP, you generally want to reuse as much code as possible, allowing subcontrollers to inheirit code from parent controllers.

CoolFactor's proposed View library thread is here:

http://ellislab.com/forums/viewthread/49910/