cbhjatarj 发表于 2010-11-2 22:03:14

$this->load->vars($array)怎么传到视图

比如我想加载配置文件中的网站URL地址.然后传到视图去.不用每个函数都去写.

lamtin 发表于 2010-11-2 22:58:15

能不能说明白一点?

keeponac 发表于 2010-11-2 23:36:25

扩展Controller,声明一个类变量$data[],构造的时候将共有信息放到$data里,然后继承扩展类

.\system\application\libraries\MY_Controller.php

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

class MY_Controller extends Controller
{
        protected $data = array();
        protected $controller_name;
        protected $action_name;
   
        public function __construct()
        {
                parent::__construct();
                $this->load_defaults();
        }

       
        protected function load_defaults()
        {
                //$this->class_name = strtolower(get_class($this));
                $this->controller_name = $this->router->fetch_directory() . $this->router->fetch_class();
              $this->action_name = $this->router->fetch_method();
              $this->data['title'] = $this->controller_name;
        }
       
        protected function render($template='main')
        {
                $view_path = $this->controller_name . '/' . $this->action_name . '.tpl.php';
                if (file_exists(APPPATH . 'views/' . $view_path))
                        $this->data['content'] .= $this->load->view($view_path, $this->data, true);
                $this->load->view("layouts/$template.tpl.php", $this->data);
        }
       
        protected function add_css($filename)
        {
              $this->data['css'] .= $this->load->view("partials/css.tpl.php", array('filename' => $filename), true);
        }
}

cbhjatarj 发表于 2010-11-3 08:01:20

回复 2# lamtin


    就是指南里说的这个"$this->load->vars($array)
这个函数以一个关联数组作为输入参数,将这个数组用PHP的extract函数, 转化成与这个数组对应的变量.这个函数如果用第二个参数,将产生和上面的$this->load->view()相同的结果 .你之所以要单独用这个函数也许是因为,你想在控制器的构造函数中设置一些全局变量,以使这些变量在任意函数调用的视图(view)里能够用上.你能多次调用这个函数.数组数据被缓存并被并入一个数组,用来转化成变量."

你想在控制器的构造函数中设置一些全局变量,以使这些变量在任意函数调用的视图(view)里能够用上
就是这个我不知道怎么操作.

cbhjatarj 发表于 2010-11-3 08:01:59

谢谢keeponac的回复但.你的方法我确实看不懂.我刚入门的

sonic 发表于 2010-11-4 17:59:23

$this->load->view('file_name',$data);
页: [1]
查看完整版本: $this->load->vars($array)怎么传到视图