n4mi n4mi - 2 months ago 9
PHP Question

Codeigniter best way to handle controller calling views

i just want to know if this is a good way in adding fragmented views.

lets say "header.php" is like this

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Admin Theme v3</title>
some links..
</head>
</html>


then "body.php" is like this

<!DOCTYPE html>
<html>
<body>
lots of stuff..
</body>
</html>


lastly "scripts.php"

<!DOCTYPE html>
<html>
<body>
some scripts..
</body>
</html>


then in my "MyController.php"

$this->load->view('header');
$this->load->view('body');
$this->load->view('scripts');

Answer

The best way I find is create a default view.

views > default_view.php

views > includes > header_view.php

views > includes > footer_view.php

views > information > contact_view.php

On that view

<?php

$this->load->view('includes/header_view');

$this->load->view($content_page);

$this->load->view('includes/footer_view');

?>

Then on the controller to load view this way you do not have to load the header and footer views all the time.

Following the Codeigniter StyleGuide

Filename: Example.php

<?php

class Example extends CI_Controller {

public function index() {

   // Add any other variables

   $data['content_page'] = 'information/contact_view'; // This will be your content page example 

   $this->load->view('default_view', $data);

}

}
Comments