[英]Why I can not pass a variable between controller functions in codeigniter
I have this in a codeigniter controller: 我在codeigniter控制器中有这个:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller {
public $idioma;
public function index() {
parent::__construct();
// get the browser language.
$this->idioma = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
$data["idioma"] = $this->idioma;
$this->load->view('inicio', $data);
}
public function hello(){
$data["idioma"] = $this->idioma;
$this->load->hello('inicio', $data);
}
}
Inicio view: 初始视图:
<a href="test/hello">INICIO <?php echo $idioma ?></a>
hello view: 你好视图:
Hello <?php echo $idioma ?>
The inicio view works great, but when the hello view is loaded there's nothing displayed. inicio视图的效果很好,但是在加载hello视图时,没有任何显示。 Any idea why this is not working?
知道为什么这行不通吗?
If you wish to set a class property automatically you would do it in the constructor, not in index(). 如果希望自动设置类属性,则可以在构造函数中而不是在index()中进行设置。 index() does not run before other methods if they are called directly.
如果直接调用index(),则它们不会在其他方法之前运行。 In your case I assume you're calling hello via the url as test/hello
在您的情况下,我假设您是通过url以test / hello方式打招呼的
class Test extends CI_Controller {
public $idioma;
public function __construct(){
parent::__construct();
// get the browser language.
$this->idioma = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
}
public function index() {
$data["idioma"] = $this->idioma;
$this->load->view('inicio', $data);
}
public function hello(){
$data["idioma"] = $this->idioma;
$this->load->hello('inicio', $data);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.