开发者

Best way to construct a website application in code igniter

开发者 https://www.devze.com 2023-03-25 14:12 出处:网络
How do people construct websites with cake/CI ect... for easy maintenance on the html? I can put each of the sections in its own view file and make the website that way:

How do people construct websites with cake/CI ect... for easy maintenance on the html?

I can put each of the sections in its own view file and make the website that way:

<div id="header"></div> <!-- header_view.php -->
<div id="content"> <!-- header_view.php -->
    <div id="left_column"></div> <!-- page_x_view.php -->
    <div id="center_column"></div> <!-- page_x_view.php -->
</div>
<div id="footer"></div> <!-- footer_view.php -->

But each page_x_view.php file would contain

<div id="left_column"><!-- Content --></div>
<div id="center_column"><!-- Content --></div>

And I'm duplicating these items through each of the files, so if I need to开发者_如何学编程 change the column structure then it is not easy.

Hopefully I am clear.


I have a controller caled MY_Controller which has a method that renders the complete page. I extend all my controllers from this main controller. HOw this helps? My main controllers takes a view and embedds it in the main content area of page and assembles a complete page. This controller takes header, footer, sidebar views and does all the mambo jumbo. Its very easy to develop such a system in CI. Some this call two step or multiple views. So if some random day I have to change layout of my page I just need to look at MY_Controller.

Cake on the other side uses layouts. I have done just one project in CakePHP so am not that expert but you can achieve the same effect in any framework. Here is how I do it in CI

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

class MY_Controller extends CI_Controller 
{

    public function __construct() 
    {
        parent::__construct();
        log_message('debug', 'Controller Library '.__CLASS__ . ' ('. __FILE__ .') loaded.');
        $this->properties['viewPath'] = $this->config->item('viewPath');
        $this->setPageMetaData();
        $this->setFavIcon();

    }

    public function render($viewData = null, $data=null)
    {
        $data = array(
            'headerLayout' => $this->printHeaderLayout(array_merge($this->properties, (isset($data['headerLayout'])?$data['headerLayout']:array()))),
            'leftLayout' => $this->printLeftLayout(array_merge($this->properties, (isset($data['leftLayout'])?$data['leftLayout']:array()))),
            'rightLayout' => $this->printRightLayout(array_merge($this->properties, (isset($data['rightLayout'])?$data['rightLayout']:array()))),
            'footerLayout' => $this->printFooterLayout(array_merge($this->properties, (isset($data['footerLayout'])?$data['footerLayout']:array()))),
            'containerLayout' => $viewData,
        );
        this->load->view($this->properties['viewPath'].'layout/layout.php', $data);
    }

    public function setPageMetaData($pageMetaData=null) 
    {
        $this->properties['pageTitle'] = isset($pageMetaData['pageTitle'])? $pageMetaData['pageTitle'] : $this->config->item('pageTitle');
        $this->properties['pageKeywords'] = isset($pageMetaData['pageKeywords'])? $pageMetaData['pageKeywords'] : $this->config->item('pageKeywords');
        $this->properties['pageDescription'] = isset($pageMetaData['pageDescription'])? $pageMetaData['pageDescription'] : $this->config->item('pageDescription');
    }

    public function setFavIcon($favIcon=null) 
    {
        $this->properties['favIcon'] = (null !== $favIcon) ? $favIcon : $this->config->item('favIcon');
    }

    public function printHeaderLayout($data=null)
    {
        return ($this->load->view($this->properties['viewPath'].'layout/header', $data, true));
    }

    public function printFooterLayout($data=null)
    {
        return( $this->load->view($this->properties['viewPath'].'layout/footer', $data, true));
    }

    public function printLeftLayout($data=null)
    {
        return($this->load->view($this->properties['viewPath'].'layout/left', $data, true));
    }

    public function printRightLayout($data=null)
    {
        return($this->load->view($this->properties['viewPath'].'layout/right', $data, true));
    }
}

Do note that this is not the exact code. I had to modify it for you, so do not blindly user it. If you know CI you will understand that I have setup paths to view in a config file. This helps me in setting up two totally different themes and use same controller. I can also add authentication layer which will based on user authentication/cookies can show a login or logout link in header. This is a template which I keep change and I extend all my controllers from MY_Controller and use in my controllers I simply do

 $viewDataForForm = $this->load->view($this->properties['viewPath'].'homepage/some-form', array(), true);
 $viewDataForContent = $this->load->view($this->properties['viewPath'].'homepage/some-content', array(), true);
 $this->render($viewDataForForm.$viewDataForContent);

HTH!


Codeigniter and CakePHP take advantage of the Model View Controller configuration. They separate the database queries and data processing from the views. This provides an easy to use and easy to maintain way of coding. Multiply controllers can use the same view which helps cut down on the amount of code written and the complexity. Methods in the models can be reused which reduces bugs and amount of code written. And controllers provide and easy to follow way of reading and writing code. I am not sure if I answered your question but comment on my answer if you need and more explanation.

0

精彩评论

暂无评论...
验证码 换一张
取 消