Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter-在调用控制器期间,什么样的变量可以工作?_Php_Codeigniter - Fatal编程技术网

Php CodeIgniter-在调用控制器期间,什么样的变量可以工作?

Php CodeIgniter-在调用控制器期间,什么样的变量可以工作?,php,codeigniter,Php,Codeigniter,我想问的第一件事是控制器是如何工作的?假设我有一个控制器: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Home extends CI_Controller { public static $groupId; public function __construct() { } public function index() {

我想问的第一件事是控制器是如何工作的?假设我有一个控制器:

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

class Home extends CI_Controller {

    public static $groupId;

    public function __construct() {

    }

    public function index() {
        $this->load->model('product_model');

        $data['new_products'] = $this->product_model->get_new_products();
        $data['featured_products'] = $this->product_model->get_featured_products();
        $data['sale_products'] = $this->product_model->get_sale_products();

        $data['template'] = 'index';
        $data['title'] = 'Home';

        $this->load->view('master', $data);
    }

    public function group($groupId) {
        $this->load->model('group_model');
        $this->load->model('product_model');

        $groupId = array_pop(explode('-', $groupId));
        self::$groupId = $groupId;

        $data['groupName'] = $this->group_model->get_group_name($groupId);
        $data['allGroupWithQuantity'] = $this->group_model->get_group_with_quantity();
        $data['products'] = $this->product_model->get_products_by_group($groupId);

        $data['template'] = 'group';
        $data['title'] = $data['groupName']->groupName;

        $this->load->view('master', $data);
    }

    public function test() {
        echo seft:$groupId;
    }

}

/* End of file home.php */
/* Location: ./application/modules/front/controllers/home.php */
当我访问http://localhost:8080/ci/ 和http://localhost:8080/ci/television 然后输入http://localhost:8080/ci/test/,我得到白色屏幕。如果第一次在controller中调用了use-method,第二次,我认为controller不需要从group-method重新加载,我为$groupId设置了值,在测试方法中我可以很容易地得到它,但我不能。也许当我调用测试方法时,控制器被重新加载


我想问的第二件事是,如何通过其他方法传递$groupId?记住$groupId不存储在控制器中,我从url获取它。

您需要像这样调用控制器:

http://localhost:8080/ci/home/group/12
include_once 'group.php';
class Home extends CI_Controller {



public function __construct() {

}
public function index() {
    $this->load->model('product_model');

    $data['new_products'] = $this->product_model->get_new_products();
    $data['featured_products'] = $this->product_model->get_featured_products();
    $data['sale_products'] = $this->product_model->get_sale_products();

    $data['template'] = 'index';
    $data['title'] = 'Home';

    $this->load->view('master', $data);
}
public function group($groupId) {
    $this->load->model('group_model');
    $this->load->model('product_model');

    $groupId = array_pop(explode('-', $groupId));
    Group::$groupId = $groupId;

    $data['groupName'] = $this->group_model->get_group_name($groupId);
    $data['allGroupWithQuantity'] = $this->group_model->get_group_with_quantity();
    $data['products'] = $this->product_model->get_products_by_group($groupId);

    $data['template'] = 'group';
    $data['title'] = $data['groupName']->groupName;

    $this->load->view('master', $data);
}
public function test() {

    echo Group::$groupId;
}

}
?>
其中12是您的groupid,home是控制器的名称。使用会话在组函数中存储$groupId:

$this->CI->session->set_userdata('groupId', $groupId);
要在测试中加载$groupId,请使用以下代码行:

$groupId = $this->CI->session->userdata('groupId');
另一个解决方案是根据要求创建一个名为group的静态类,尽管这不是最佳实践:

<?php 
class Group
{
public static $groupId = 12;


}?>
有关控制器的详细信息,请访问此网站:


我知道。我总是可以从url localhost:8080/cifirst/telephone-1.html获取$groupId。我接受你的回答,但很抱歉,我不想使用会话。我在asp.NETMVC中看到,如果我在控制器中这样做:私有静态字符串关键字;公共静态字符串关键字{get{return Keyword;}set{Keyword=value;}},如果我用任何值分配关键字,例如group方法中的groupId,我可以刷新页面,但是关键字值不会丢失。我想这样做。