【问题标题】:Codeigniter can't use this->uri->segment(3) as value in functionCodeigniter 不能使用 this->uri->segment(3) 作为函数中的值
【发布时间】:2012-11-28 14:15:53
【问题描述】:

为什么这段代码没问题

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

我的网址是 domain: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 函数中使用它作为参数,则不起作用!

下一个方式生成的页面: 控制器 admin_schedule 具有渲染视图 index.php 的函数 index。 在那个视图中,我使用 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);
        }
    }

请帮助理解这个问题。

【问题讨论】:

  • 您是否尝试将其分配给一个变量并回显它以查看发生了什么?
  • 是的,当然:$username1 = "'".$this->uri->segment(3)."'"; print_r($username1);这应该打印“vika”。
  • 如果是这样,您的最后一个问题已经得到解答。这给我们留下了主要问题。您是否尝试过使用变量而不是直接将值传递给方法(看起来很奇怪,但在某些情况下会导致引用问题)。试一试。
  • 在变量 $username1 = "vika" 中传递值 'vika';给出了很好的结果,是的。如果我使用 $this->uri->segment(3),就会出现问题。我尝试使用 rsegment 代替 segment,但没有帮助。
  • 如果您解决了您的问题,请将解决方案作为答案发布,以便对其他人有所帮助。

标签: codeigniter url-routing codeigniter-2 codeigniter-routing


【解决方案1】:

我不记得原因 - 这只是你学会接受的 CI 怪癖 - 你不能直接使用 $this->uri->segment(3) 等作为参数,你需要分配它并然后按照@almix 为他的理智测试建议的那样通过。

至少我也一直很难将它直接用作论据 - 我很乐意任何人纠正我!

【讨论】:

  • 不,Brian,将其分配给参数也不起作用。 $sch_teacher_id = 111;//$this->uri->segment(3); $result=$this->Schedule_model->get_schedule_recurring_events_on_daysweek($start, $end, $sch_teacher_id);有效,但使用 $sch_teacher_id = $this->uri->segment(3);没有!
  • 你的意思是:$username1 = $this->uri->segment(3);那么使用 $username1 不起作用?
  • 嗯 ok ellislab.com/codeigniter/user-guide/libraries/uri.html 声明如果找不到该项目,它将返回“false”。
  • 我添加了有关路线的解释。
  • 可能会以某种方式使用此建议stackoverflow.com/questions/2062086/…
【解决方案2】:

我的问题解决了!

认为由于 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 选项 data 将这个值(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);
        ...
    }

这就是霹雳舞。

【讨论】:

  • 这和我们的建议是一样的,先赋值然后通过。
猜你喜欢
  • 2016-07-07
  • 2014-12-18
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
相关资源
最近更新 更多