CodeIgniter - 调用控制器期间可以使用哪种变量?

时间:2013-06-22 15:40:13

标签: 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() {
        $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/时,我会看到白屏。如果在第一次调用控制器(在控制器中使用方法),并且在第二次,我认为控制器不需要重新加载所以从组方法,我为$ groupId设置值,在测试方法中我可以轻松搞定但我不能。也许当我调用测试方法时,重新加载控制器。

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

1 个答案:

答案 0 :(得分:0)

你需要像这样召唤你的控制器:

http://localhost:8080/ci/home/group/12

其中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;


}?>

然后你的家用控制器将如下所示:     

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;
}

}
?>

有关控制器的更多信息,请访问以下网站:

http://ellislab.com/codeigniter/user-guide/general/controllers.html