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

[eluser]bisol[/eluser]
Hello,

I am new with Code Igniter / MVC and I have looked the video tutorials and lot of posts in FAQ / forum but I didn't understand how to begin with CI.

Actually, my website (http://www.beegoo.net) has some include :
- Header
- Footer
- Left part
- Search engine
- ...

I want to do this with CI but I don't know if I need to create :
- 1 controller for homepage ?
- 1 for left part, 1 for search, 1 header, .... ?

For the moment I've tested this but I need to do some view loading into other views (col_milieu will load "search_engine" and "ads").

Code:
$data = array(
            'titre' => 'Test d\'écoute',
                'header' => 'My Heading',
        'keyword' -> 'Keyword'
                );
        $this->load->view('header', $data);
        $this->load->view('accueil', $data);
        $this->load->view('col_milieu', $data)
        $this->load->view('col_droite', $data)
        $this->load->view('footer', $data);

Thanks by advance for your help

BR.
#2

[eluser]macleodjb[/eluser]
This is how i do it.

I place this in my view file
Code:
<?php
$this->load->view("header_meta");
$this->load->view("header");
$this->load->view("hormenu");
these are only view files

Then i place this inside my controller,
Code:
$data['page_title'] = "My page Title";
$data['page_desc'] = "My page Description";
$data['page_keywords'] = "My page Keywords";
$this->load->view('register', $data);
#3

[eluser]fesweb[/eluser]
This is a huge topic, and there are a lot of ways to do these things.

What you want to avoid is having to load so many views in order to generate a full page.

Ideally, you should use templates of some sort (search the forums for template libraries).

Conceptually, you want to have something like this:
Code:
// in views/templates/basic_layout.php
<html>
<head>
<title><?php if(isset($content_page_title)) echo $content_page_title; ?> - My Site Title</title>
</head>
<body>
<div id="header">
&lt;?php if(isset($content_header)) echo $content_header; ?&gt;
</div>
<div id="topnav">
&lt;?php if(isset($content_topnav)) echo $content_topnav; ?&gt;
</div>
<div id="content">
&lt;?php if(isset($content_main)) echo $content_main; ?&gt;
</div>
<div id="footer">
&lt;?php if(isset($content_footer)) echo $content_footer; ?&gt;
</div>
&lt;/body&gt;

So, in your controller you create chunks of content that you write to variables, and then you pass that info to the final layout page.

Code:
// in the controller
$id = $this->uri->segment(2);
// run a query in the model
$item_detail_data = $this->my_model->get_my_data($id);
// write the output from the detail view to a variable
// using TRUE as the third param of the load->view prevents
// CI from writing the output straight to the browser
$output['content_main'] = $this->load->view('item/details', $item_detail_data, TRUE);

// do whatever you need to fill out the rest of your chunks of content
$output['content_topnav'] = $this->load->view('includes/top_nav', '', TRUE);

// THEN load your layout view...
$this->load->view('templates/basic_layout', $output);
#4

[eluser]bisol[/eluser]
@macleodjb : Incluing views into views, this is not bad with the MVC approach ? The controller need to do this only no ?

Thx
#5

[eluser]Michael Wales[/eluser]
Quote:Incluing views into views, this is not bad with the MVC approach ? The controller need to do this only no?

No, it is acceptable and still maintain the MVC frame-of-mind.

Regardless, MVC is in place to help you. Why would you decide not to do something, just because some arbitrary guide/standard out there says you shouldn't, when you know it will pay dividends in return.
#6

[eluser]bisol[/eluser]
Thanks for the reply.

No, I just want to begin correctly Smile
#7

[eluser]macleodjb[/eluser]
i was going to say before i saw micheal's post.

I don't care if it is perfect MVC approach, what it does allow me to do is keep all aspects of the website separate from another. So if i want to change the header of my site completely, i change one file and the whole site is updated. All i do is pass my other variables through the controller. Works perfectly for me. However it may not be the best approach for someone else.
#8

[eluser]bisol[/eluser]
OK I begin with this approach.

1) Controller include the "middle_column.php"
2) In the middle_column.php" view, I include menu, ads, search engine...
#9

[eluser]macleodjb[/eluser]
if you're going to use that approach i would try to keep the site structure separate from your content unless it's directly integrated together. How i started was to lay out my website will all html code, I have a 3 column layout. I set the site structure and css so everything looked pretty, then split each part of the site up into parts, like...

header_meta
header
horizontal_menu
left_col
(i place the content here)
right_col
footer

this way i can change any aspect of the site without having to update 100 pages. I change one "view" like "footer" and it will update throughout the site.

So with this structure in place your view file only needs to be the actual content. you can break this up any way you like. Hope that helps
#10

[eluser]unsub[/eluser]
hi
i'm quite new to MVC myself, and slowly but surely fighting my way out of the procedural cocoon, hopefully to emerge object oriented... to be pretentious about it :lol:

but i have learned a lot from reading through the code of various backend prjects released here. CodeExtinguisher has some really wonderful stuff, but is above my head in a lot of ways; however i did copy my asset management from that, to some extent.

another is CodeCrafter, which uses a template class which was simple enough for even me to get a handle on, and alter into a site-wide system. if you are as new as me to this stuff, i highly recomend having a look at that. basically, just generate yourself a quick one-table crud using the codecrafter interface, and then have a look at the way it does it. it's easy to grasp how it works quite quickly, and once i did that, it was a piece of cake to come up with my own system based on that.
very cool stuff.

cheers
-g




Theme © iAndrew 2016 - Forum software by © MyBB