Welcome Guest, Not a member yet? Register   Sign In
static page -> dynamic page in 5 minutes
#1

[eluser]lucid[/eluser]
Hi everyone Wink

I have a website with.. about 15 pages, but all of them includes the same header and footer (simple include function Wink.

First, I wanna "remake it" - using this framework and I just want to confront You with my idea - as a total newbie.

I will create one controller, let's call it pages, with method view(), and it goes like that:

Code:
class Pages extends CI_Controller {
public function view($page = 'home') {

  $this->load->view('templates/header');
  $this->load->view('pages/'.$page);
  $this->load->view('templates/footer');
  
}//view

} //class

than in view/templates i create header.php and footer.php

and in view/pages/ I copy the pages html.

Than I need to change my routes, and mod_rewrite in .htaccess so calling for

domain.com/page3.php will actually call domain.com/index.php/pages/view/page3 <- I already know how to do it, it's not a problem.

and that's it. Job is done.

---

But.. I want to remake it with help of framework, because I want to create CMS for easy updating some of those pages. So next step would be to make some of those pages using database to download content.

IF I use this structure, would I be able to do it later? Because when i think of it, the only way to make some pages connecting with database would be calling another controller, exacly from first controller, so I would do it like that:


Code:
class Pages extends CI_Controller {
public function view($page = 'home') {
  $data=0;
  if ($page == "specific1") $data=$this->download("specific1");
  $this->load->view('templates/header');
  $this->load->view('pages/'.$page, $data);
  $this->load->view('templates/footer');
  
}//view

public function download($which_page) {
                
            //  here: connect to database, receive data, return it in array or whatever
} //download

} //class

Is it a good idea ?
#2

[eluser]jjDeveloper[/eluser]
Not too sure why you are declaring your data object as an integer then a string but as it states in the CI documentation when you load the view whatever "array" you pass to the load call will iterate and you will have variables named by their key value.

So to get data out of the database and make this template dynamic you would set the $data['content']['html'] object to the HTML you receive from your db query.

if you notice you would be able to have multiple html snippets added to this array as it iterates through them and echo's out each element.

I didn't try this example so sorry if its bugged but it should give you a better idea how to proceed.

controllers/pages.php

Code:
&lt;?php
class Pages extends CI_Controller {
  public function __construct() {
    parent::__construct();
    $dbContentResult = array();
    $dbContentResult[] = '<h1>Testing This Template</h1>';
    $dbContentResult[] = '<p>Here goes a paragraph</p>';
    $data['content']['html'] = $dbContentResult;
  }

  public function Index() {
    $this->load->view('template, $data);
  }
}

views/template.php

Code:
&lt;?php
$this->load->view('template/header');
$this->load->view('template/content', $content);
$this->load->view('templates/footer');

template/content.php

Code:
&lt;?php
if(isset($html)) {
    foreach ($html as $htmlData) {
        echo $htmlData;
    }
}
#3

[eluser]Iszuddin Ismail[/eluser]
If you are creating a CMS, then let's store all the HTML in the database. And have a field that we can call url_name or slug or file_call or something. The function of that field is to become the unique caller name to be used in the URL.

And in your route, you can have something like this.

Code:
$route['page/(:any)'] = "page/view/$1";

And then your view would be something like

Code:
class Page extends CI_Controller {
public function view() {
  $page_name = $this->uri->segment(2);

  $the_page = $this->page_model->get_page($page_name);

  if ($the_page !== false) {
     $this->load->view('templates/header');
     $this->load->view('pages/'.$the_page['view_to_load'], $the_page);
     $this->load->view('templates/footer');
  } else {
     redirect('404');
  }
}//view

But of course, in my example, you would need to build a page_model that handles calling page data from the database. When calling get_page using the page_model, it would return false when no page data is found.

#4

[eluser]lucid[/eluser]
Hi! Smile

Thank You for the replies. I have these problems because my first contact with CodeIgniter is at the same time my first contact with MVC (and my experience with OOP isn't great neither).

I'm creating this project like that:

controller pages - with basic method view, for normal user to view content of the page

controller admin - with admin-side of the website. It has lots of methods - login methods, adding information, deleting, updating... I created it all under one controller because I wanted to have .../admin/... in url. so basic method to view admin-side website is .../admin/view/index etc. BUT if user isn't logged in he is redirected to ../admin/login. Wink



#5

[eluser]jjDeveloper[/eluser]
Instead of using your download function in a controller it would be better to separate your database logic into a model. This not only would adhere to the MVC principals but it would also enable you to reuse that logic across multiple controllers, or even multiple applications.




Theme © iAndrew 2016 - Forum software by © MyBB