Welcome Guest, Not a member yet? Register   Sign In
Please help Views and routes
#1

[eluser]Sveeaa[/eluser]
Sorry for this. i really dont know what to search for i tried for 2 days.

I wanna know have to changes views with links or buttons.
Code:
function index()
    {

        $layout_data['pageTitle'] = "mysite";

        
        $this->load->view('view_header', $layout_data);
        $this->load->view('view_page1');
        $this->load->view('view_footer');
    }
this is what my controller looks like right now and i only wanna change the view_login
to maybe view_page2
I wanna keep the header and foot always.
my header loads html body and my footer closes them.
so my view_page is only <div> and stuffs like that.
or do i have to have header och footer in some kind of global controller?

and routes i saw that you can make your own ones.
$route['default_controller'] = "page1"; <-defalt one;
if i would make one like
$route['cool'] = "page2"; <-defalt one; <- how can i use it?

sorry for beeing a newbie but im trying to understand it.

best regards Svea
#2

[eluser]whobutsb[/eluser]
Hi there,
You might want to try this strategy. Create a view file called "template.php".
In your template make this the skeleton of your page that will have all the normal HTML elements:

Code:
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;&lt;?= $pageTitle; ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
    
    &lt;?php $this->load->view('view_header'); ?&gt;
    
    &lt;?php $this->load->view($view_body); ?&gt;
    
    &lt;?php $this->load->view('view_footer'); ?&gt;
&lt;/body&gt;


&lt;/html&gt;


In your controller you could write it like this:

Code:
function index(){
    
    $data['pageTitle'] = 'mysite';
    $data['view_body'] = 'view_page1';
    
    $this->load->view('template', $data);

}


This is typically how I organize my CI projects.
#3

[eluser]Sveeaa[/eluser]
thanks for that help. but i dont think thats what i really were looking for.
I wanna be able to change view file and still keep the structure of the site.
and dont have like structure in all my view files.
#4

[eluser]cahva[/eluser]
I dont know why people want to separate header and footer from the layout. I mean, how often does the structure of header and footer actually change? Not very often.

Using simple layout(or template whatever you wanna call it) where you just put place holders will be just as good:
Code:
<!DOCTYPE HTML>
&lt;html lang="en-US"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;&lt;?=$title ?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    <div id="nav">
        &lt;?=$nav ?&gt;
    </div>
    <div id="content">
        &lt;?=$content ?&gt;
    </div>
&lt;/body&gt;
&lt;/html&gt;
In this overly simplified example, $title and $content are the things that would be changed in controllers method. For repetive stuff(like $nav) you want to use base controller so you dont have to define it in every single controller's constructor or method.

Best way to use base controllers is described in this link(this must be 1000 time I post this link):

http://philsturgeon.co.uk/news/2010/02/C...ing-it-DRY

Base(using the __autoload() magic function described in the link) and page controllers would be something like this:

libraries/Front_controller.php
Code:
class Front_controller extends Controller {
    
    var $data;
    
    function __construct();
    {
        parent::__construct();
        
        $this->data['nav'] = $this->load->view('navigation','',TRUE);
    }
}

controllers/somepage.php
Code:
class Somepage extends Front_controller {

    function __construct()
    {
        parent::__construct();
    }
    
    function index()
    {
        $this->data['title'] = 'Some page';
        $this->data['content'] = $this->load->view('somepage','',TRUE);
        
        $this->load->view('layout',$this->data);
    }
    
    function foo()
    {
        $this->data['title'] = 'Foo';
        $this->data['content'] = $this->load->view('somepage/foo','',TRUE);
        
        $this->load->view('layout',$this->data);
    }
}

Ofcourse normally you would have some loading of libraries and models +other stuff but thats the basic idea. Using separate header and footer views is just confusing as 99.7% of the time they will be the same so why not put them to main layout. And if you need different header or footer, just create a new layout view file.
#5

[eluser]Sveeaa[/eluser]
Thank you.
sorry that you have to post it so many times.

yeah i kinda understood that you have to do it that way. and only think why i wanted to have seperate healder and footer where because i thought i could change the "main" view easier.

What i really wanna now is how do i change the content.


Code:
function index()
    {
        $this->data['title'] = 'Some page';
        $this->data['content'] = $this->load->view('somepage','',TRUE);
        
        $this->load->view('layout',$this->data);
    }
    
    function foo()
    {
        $this->data['title'] = 'Foo';
        $this->data['content'] = $this->load->view('somepage/foo','',TRUE);
        
        $this->load->view('layout',$this->data);
    }
}

how could a navigation look to get to that function foo. :S

best regards Svea
#6

[eluser]Sveeaa[/eluser]
Quote:What i really wanna now is how do i change the content.

that shouldnt be there.

i wanna how how to navigate to function foo.

i dont know how to explain myself

when you enter my site all you will see is a login screen/view
and when you press login enter another view/screen
that i cant figure out how to do..

best regards Svea




Theme © iAndrew 2016 - Forum software by © MyBB