Codeigniter libraries params

raogr8fs  于 2022-10-22  发布在  PHP
关注(0)|答案(1)|浏览(162)

Sometime ago i added some params to a library, and it worked just fine ... i dont know if i messed up with some configuration setting or what, right now i cant pass the exact same params.
The library Template:

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

    class Template {

        public $config = array();

        public function __construct($params = NULL)
        {   

                echo '<pre>'; print_r($params); echo '</pre>';
                /*This should print the array (and it's printing nothing):

                Array
                (
                     [menus] => Array
                       (
                            [Administradores] => index
                            [Logs] => logs
                       )

                )*/

            if ($params){
                foreach ($params as $key => $value){
                    $this->config[$key] = $value;
                }
            }

        }

        public function cms_template($pagina=NULL, $modulo=NULL, $titulo=NULL, $data=NULL, $dir='backoffice')
        {
            $CI =& get_instance();
            $CI->load->library('session');
            if (isset($this->config['menus']))
                $menus = $this->config['menus'];
            else
                $menus=NULL;

            $dados['dados'] = array(
                'id_administrador' => $CI->session->userdata('id_administrador'),
                'email' => $CI->session->userdata('email'),
                'nome' => $CI->session->userdata('nome'),
                'modulo' => $modulo,
                'titulo' => $titulo,
                'menus' => $menus
            );

            $CI->load->view($dir.'/topo', $dados);
            $CI->load->view($dir.'/'.$pagina, $data);
            $CI->load->view($dir.'/rodape');    
        }

The controller:

public function __construct() {
        parent::__construct();
        $this->auth_model->verificaLogin('id_administrador','backoffice/login');
        $this->load->model('administradores_model');
        $params=array(
            'menus' => array(
                'Administradores'=>'index',
                'Logs'=>'logs'
            )
        );

        $this->load->library('template', $params);
    }

When i run it, it throws me n error on the view:

<?php
            foreach ($dados['menus'] as $key => $value) {
                echo '<li>'.active_anchor('/'.$this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$value, $key, '/'.$this->uri->uri_string()).'</li>';
            }
            ?>

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()

iszxjhcz

iszxjhcz1#

About your last comment on why autoloading this library it stops working! This has nothing to do with CodeIgniter, but PHP limitation. There's a name conflict probably.
I'm not sure but remember that PHP has a limitation, your classes must have a unique name. I don't know if "template" is a CI reserved name but try a different name. If this is the case to solve it, you must have a good naming scheme, that's why CI uses suffix and prefixes. Or namespaces, but CI doesn't support it.

相关问题