【问题标题】:Message: Undefined property: Todo::$uri消息:未定义的属性:Todo::$uri
【发布时间】:2019-07-15 17:39:54
【问题描述】:

我使用 codeigniter。

当我尝试获取 url 中的参数时出现错误

<?php

class Todo extends CI_Controller{
public function __construct() {
    parent::__construct();



}

public function index(){
$this->load->view('index.php');

}
public function blog(){
$this->load->view('arsiv');

}
public function test($param){
    $this->load->view('test');
    $a =  $this->uri->segment(2);


}
}

当我尝试去时:http://localhost/cod/index.php/todo/test/123

遇到 PHP 错误
严重性:通知
消息:未定义的属性:Todo::$uri
文件名:控制器/Todo.php
行号:21
回溯:
文件:/opt/lampp/htdocs/cod/application/controllers/Todo.php
线路:21
函数:_error_handler
文件:/opt/lampp/htdocs/cod/index.php
线路:315
函数:require_once

遇到未捕获的异常
类型:错误
消息:在 null
上调用成员函数 segment() 文件名:/opt/lampp/htdocs/cod/application/controllers/Todo.php
行号:21
回溯:
文件:/opt/lampp/htdocs/cod/index.php
线路:315
函数:require_once

【问题讨论】:

  • 嗨 @mehmtee10,欢迎来到 Stack Overflow。这个问题需要编辑。为了提高你的问题质量,我邀请你阅读How do I ask a good question
  • uri 类是自动加载的,所以我不确定你是如何得到这个错误的,因为你已经扩展了 ci_controller。这是您使用的确切代码吗?

标签: php codeigniter


【解决方案1】:

请确认您的配置函数中启用了 URL 助手

尝试使用 $this->uri->get_segment(1);

尝试使用

$CI =& get_instance(); 
$this->CI->uri->segment('2')

【讨论】:

    【解决方案2】:

    您必须在配置文件夹中的 codigniter autoload.php 文件中加载帮助程序

    $autoload['helper'] = array('url');
    

    【讨论】:

      【解决方案3】:

      尝试使用

      config文件夹中的codigniter autoload.php文件

      $autoload['helper'] = array('url');
      
      Url : http://localhost/laravel/users/abc
      $product_id = $this->uri->segment(2);
      Result : abc
      

      阅读https://www.codeigniter.com/user_guide/libraries/uri.html

      【讨论】:

      • 请在您的代码中添加一些解释,以便其他人可以从中学习
      猜你喜欢
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      相关资源
      最近更新 更多