Return to Snippet

Revision: 63387
at May 2, 2013 02:54 by phptrampos


Updated Code
<?php  
//*************************************
//Arquivo: application/config/hooks.php 
//*************************************

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

$hook['post_controller_constructor'] = array(
		"class" => "frontController",
		"function" => "onApplicationStart",
		"filename" => "frontController.php",
		"filepath" => "hooks"
	);

$hook['post_controller'] = array(
		"class" => "frontController",
		"function" => "onApplicationEnd",
		"filename" => "frontController.php",
		"filepath" => "hooks"
	);	
?>

<?php
//**********************************************
//Arquivo: applicatoin/hooks/frontController.php
//**********************************************
class frontController {

    function onApplicationStart() {
            $CI =& get_instance();
            if(!$this->isAjax() && !@in_array($CI->uri->rsegment(2), $CI->noRender))
                $CI->load->view("_header");
    }

    function onApplicationEnd() {
            $CI =& get_instance();

            if(!$this->isAjax() && !@in_array($CI->uri->rsegment(2), $CI->noRender))
                $CI->load->view("_footer");
    }

    function isAjax() {
        if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest") {
                return true;
        } else {
                return false;
        }
    }

}
?>

Revision: 63386
at May 2, 2013 02:48 by phptrampos


Initial Code
<?php  
//*************************************
//Arquivo: application/config/hooks.php 
//*************************************

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

$hook['post_controller_constructor'] = array(
		"class" => "frontController",
		"function" => "onApplicationStart",
		"filename" => "frontController.php",
		"filepath" => "hooks"
	);

$hook['post_controller'] = array(
		"class" => "frontController",
		"function" => "onApplicationEnd",
		"filename" => "frontController.php",
		"filepath" => "hooks"
	);	
?>

<?php
//**********************************************
//Arquivo: applicatoin/hooks/frontController.php
//**********************************************
class frontController {

    function onApplicationStart() {
            $CI =& get_instance();
            if(!$this->isAjax() && !@in_array($CI->uri->rsegment(2), $CI->noRender)){
                $CI->load->view("_header");
            }
    }

    function onApplicationEnd() {
            $CI =& get_instance();

            if(!$this->isAjax() && !@in_array($CI->uri->rsegment(2), $CI->noRender)){
                $CI->load->view("_footer");
    }

    function isAjax() {
        if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest") {
                return true;
        } else {
                return false;
        }
    }

}
?>

Initial URL


Initial Description
Este é um hook que utilizo para evitar de carregar headers e footers em cada view que carrego nos meus projetos. Tem me sido muito útil desde que comecei a utiliza-lo.

O que ele faz? --------

Basicamente a cada chamada ($this->load->view('arquivo');) que faço nos controllers, o hook irá carregar junto 2 outros views um header e um footer (respectivamente os arquivos _header.php e _footer.php na pasta /view). Um antes da minha chamada , no caso o _header, e um depois que a view principal carregar, no caso o _footer.

Como usar? --------

1º - Modifique o arquivo /config/hooks.php com o código informado mais abaixo;

2º - Crie o arquivo frontController.php na pasta /hooks e cole o código informado no Source;

3º - Habilite os hooks no arquivo config.php mudando o parâmetro: $config['enable_hooks'] = TRUE;

4º - Crie 2 arquivos na pasta /application/views/_header.php e _footer.php e os modifique de acordo com o seu site. Colocando o conteúdo de Header (meta tags, styles e javascripts fixos) no arquivo _header.php até a abertura da tag body;

No arquivo _footer.php o fechamento do </body></html> e qualquer outro conteúdo que queira deixar fixo no rodapé do seu site.


Chamadas AJAX:
Não se preocupe com as suas chamadas AJAX. Este Hook irá perceber isso e não irá carregar as views.

Se tiver dúvidas, entre em contato comigo pelo email : phptrampos[at]gmail[dot]com

Initial Title
Hooks - frontController

Initial Tags


Initial Language
PHP