Codeigniter不能将this-> uri-> segment(3)用作函数中的值

问题描述 投票:0回答:2

为什么这段代码没问题

$this->sch_teacher_id = $this->ion_auth->user_by_username("vika")->row()->id;

但这不起作用?

$this->sch_teacher_id = $this->ion_auth->user_by_username($this->uri->segment(3))->row()->id;

我的网址是域名:8888 / admin_schedule / teacher / vika

route.php包含

$route['admin_schedule/teacher/(:any)'] = "admin_schedule/index/$1";

我在函数__construct()中使用代码行,并且它的结果在另一个控制器函数中使用,因为3个函数使用此结果。如果我在其中一个函数中移动此代码并使用$ this-> uri-> segment(3)那么我得到的不是'vika的课程,而是我自己的课程,所以

public function user_by_username($username = FALSE)
    {
        $this->trigger_events('user');

        //if no id was passed use the current users id
        $username || $username = $this->session->userdata('username');

        $this->limit(1);
        $this->where($this->tables['users'].'.username', $username);

        $this->users();

        return $this;
    } 

效果很好。但是$ this-> uri-> segment(3)如果在user_by_username函数中使用它作为参数,则不起作用!

页面生成的下一个方法:controller admin_schedule具有渲染视图index.php的函数索引。在该视图中我使用javascript,从admin_schedule中调用另一个函数controller = get_schedule_db_recurring_events_on_daysweek这样:

...
{
                url: '/admin_schedule/get_schedule_db_recurring_events_on_daysweek/',//"<?echo $data_path?>",
                backgroundColor: 'red',
            }

并在控制器中

function get_schedule_db_recurring_events_on_daysweek()
    {   
        $start = date('Y-m-d H:i', $this->input->get('start')); 
        $end = date('Y-m-d H:i', $this->input->get('end')); 
        $sch_teacher_id = $this->uri->segment(3); // <--- this doesn't work, but $sch_teacher_id = 111 works perfectly
        $result=$this->Schedule_model->get_schedule_recurring_events_on_daysweek($start, $end, $sch_teacher_id);
        $count=count($result);      
        if($count>0){
                echo json_encode($result);
        }
    }

请帮助理解这个问题。

codeigniter url-routing codeigniter-2 codeigniter-routing
2个回答
0
投票

我记不起原因了 - 这只是你学会接受的CI怪癖 - 你不能直接使用$ this-> uri-> segment(3)等作为参数,你需要分配它然后传递正如@almix建议他进行理智测试。

至少我也一直很难直接使用它作为一个论点 - 所以我很乐意让任何人纠正我!


0
投票

我解决了我的问题!

认为因为ajax调用而出现问题。所以当我在控制器中编码时

function __construct()
 {
        parent::__construct();
        ...

        $this->sch_teacher_row = $this->ion_auth->user_by_username($this->uri->segment(3))->row();
        $this->data['sch_teacher_id'] = $this->sch_teacher_row->id;
        $this->data['subtitle'] = $this->sch_teacher_row->username;          
 } 

接下来我有正确的值('vika'),或者在视图文件中更精确的vika的id('911'),但在我的控制器的其他功能中没有。但我现在可以使用jQuery $ .ajax选项数据从视图中将此值(sch_teacher_id)传递给此控制器:

eventSources: [
...         
{
                url: '/admin_schedule/get_schedule_db_recurring_events_on_daysweek/',//"<?echo $data_path?>",
                type: 'GET',
                data: {sch_teacher_id: sch_teacher_id},
                backgroundColor: 'red',
            }  
        ],

接下来(在山的另一边)捕获这个GET参数并将其踢到模型函数:

function get_schedule_db_recurring_events_on_daysweek()
    {   
        ...
        $sch_teacher_id_from_view = $this->input->get('sch_teacher_id');
        $result=$this->Schedule_model->get_schedule_recurring_events_on_daysweek($start, $end, $sch_teacher_id_from_view);
        ...
    }

这是所有的霹雳舞。

© www.soinside.com 2019 - 2024. All rights reserved.