【问题标题】:how to use router inside codeigniter helper by ci instance like $CI->router->fetch_method()如何通过 ci 实例在 codeigniter 助手中使用路由器,如 $CI->router->fetch_method()
【发布时间】:2016-04-12 12:25:41
【问题描述】:

我尝试在自定义助手中创建新方法,

if ( ! function_exists('active_link'))
{
    function active_link($method_name)
    {
        if($CI->router->fetch_method()==$method_name){
            return 'active';
        }               
        return 'none';
    }   
}

代码不起作用,因为我无法通过 CI 实例获取路由器。

遇到 PHP 错误

严重性:错误

消息:调用成员函数 fetch_method() on null

文件名:helpers/smart_helper.php

行号:8

【问题讨论】:

  • 我想你忘了$CI =& get_instance();里面的助手!!
  • 不,我已经这样做了,但它不起作用。
  • <?php if ( !defined('BASEPATH')) exit('不允许直接脚本访问'); $CI =& get_instance(); if (!function_exists('active_link')) { function active_link($method_name) { if($CI->router-&gt;fetch_method()==$method_name){ return 'active'; } 返回“无”; } }

标签: php codeigniter helper


【解决方案1】:

使用

$this->router->fetch_method();

代替

$CI->router->fetch_method()

【讨论】:

    【解决方案2】:

    我通过将路由器的请求移动到视图并将两个参数传递给函数来解决了这个问题: 在助手中

    <?php    
        if ( ! function_exists('active_link'))
        {
            function active_link($feched_method,$method_name)
            {
                if($feched_method==$method_name){
                    return 'active';
                }               
                return false;
            }   
        }
    

    在视图中:

    <?php echo active_link($this->router->fetch_method(),'index');?>
    

    【讨论】:

      猜你喜欢
      • 2011-07-15
      • 2012-01-10
      • 2019-12-14
      • 2014-05-23
      • 2012-06-21
      • 2018-03-25
      • 2014-12-23
      • 1970-01-01
      • 2014-10-27
      相关资源
      最近更新 更多