Pass a query array to all Codeigniter pages

Asked

Viewed 536 times

3

Has a view that repeats itself in all others through a

$this->load->view('menu');

would have a way to load the dynamic menu query in all other views with the line above? If I do this on all controllers, of course sure:

$dados['menus'] = $this->Menu_model->menus();
$this->load->view('layout/menu', $dados);

... but I wonder if there’s a way not to repeat this in the controllers and use the line above in just one location, as if it were a constant.

Thanks!

1 answer

2


You can create a controller class MY_Controller extending the class CI_Controller Codeigniter and then make all your controllers extend your custom class. The logic is to create a base file that has everything that will repeat on every page of your site, namely static contents (e.g., headers, menus and footers), this base file I call v_base. From then on, every time you call a new page, it would trigger the v_base and within that file, would be called its contents.

In this custom class, you would have more or less the following configuration:

class MY_Controller extends CI_Controller {

    private $dadosPagina; // Informações (variáveis) que serão usados na página carregada
    private $v_base; // Arquivo base da interface (menus fixos, cabeçalho e rodapé da página)

    public function __construct() {
        parent::__construct();
        $this->dadosPagina = array();
        $this->v_base = array();
    }

    /**
     * Adiciona dados à página que será carregada, como variáveis
     * @param String $nome Identificador da variável
     * @param String $valor Valor a ser usado a partir da chamada de seu identificador
     */
    protected function addDadosPagina($nome, $valor) {
        $this->dadosPagina[$nome] = $valor;
    }

    /**
     * Adiciona um título à página que será carregada
     * @param String $valor Título para a página
     */
    protected function addTituloPagina($valor) {
        $this->dadosPagina['tituloPagina'] = $valor;
    }

    /**
     * Carrega uma página do site
     * @param String $pagina Caminho do arquivo da página (path) (sem a extensão)
     */
    protected function mostrarPagina($pagina) {            
        $this->v_base['conteudo'] = $this->load->view($pagina, $this->dadosPagina, true);
        $this->load->view('v_base', $this->v_base);
        $this->dadosPagina = array(); // zerar atributo dadosPagina
        $this->v_base = array(); // zerar atributo v_base
    }

}

The call of these functions, would be thus:

class Paginas_Usuarios extends MY_Controller {

    public function __construct() {
        parent::__construct();
    }

    /**
     * Carregar página de cadastro de usuário.
     */
    public function cadastrar_usuario() {
        $this->addTituloPagina('Cadastro de Usuário');
        $this->mostrarPagina('usuario/v_cadastrar');
    }
}
  • I understand, but in my case the content is dynamic. The menu and submenu is mounted from the pages registered in the database and are loaded according to the permissions of the user. But it was useful to me, in everything else (static content) I used the logic you gave me.

  • But using the logic of "static" menus does not restrict you to changing the contents of the menu according to the permission. The only thing that is static is the structure of HTML.

  • If you pass in the dataPagina, the permissions of the logged-in user, you can perfectly use in the menu view, this variable and check :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.