简体   繁体   English

Codeigniter-如何仅在默认控制器和方法中调用辅助方法

[英]codeigniter - how to call helper method only in default controller and method

I created a helper for save user_agent in db that have a method saveVisit(); 我创建了一个用于在db中保存user_agent助手 ,该助手具有方法saveVisit();

visit_helper : visit_helper:

  function saveVisit($visitable_type='',$visitable_id='')
  {
    $CI =& get_instance();
    $CI->load->library('user_agent');
    $ip = $CI->input->ip_address();

    $userId = $CI->session->userdata('userId');
    if (!isset($userId)) {
      $userId = 0;
    }
    if ($CI->agent->is_mobile()) {
      $mobile=$CI->agent->mobile();
    }else {
      $mobile=0;
    }
    $data = array(
      'ip'=>$ip,
      'visitable_id'=>$visitable_id,
      'visitable_type'=>$visitable_type,
      'user_id'=>$userId,
      'created_at'=>dbtime(),
      'browser'=>$CI->agent->browser(),
      'version'=>$CI->agent->version(),
      'platform'=>$CI->agent->platform(),
      'mobile'=>$mobile,
      'referral'=>$CI->agent->referrer(),
    );
    $CI->db->insert('visits',$data);
  }

When i call a method like saveVisit(); 当我调用诸如saveVisit();的方法时saveVisit(); that created in my helper , everything is ok and it called where i writed ! 在我的助手中创建的,一切正常,它叫做我写的地方!

But when i call this method from $route['default_controller'] = 'display/home'; 但是当我从$route['default_controller'] = 'display/home';调用此方法时$route['default_controller'] = 'display/home'; this method will called from all of methods in all of my controllers !!! 该方法将从我所有控制器中的所有方法中调用!!!

I want to call this method only home page not all controllers/methods ! 我只想将此方法称为主页,而不是所有控制器/方法

This issue is for only my $route['default_controller'] = 'display/home'; 这个问题只针对我的$route['default_controller'] = 'display/home';

code of my controller : 我的控制器的代码:

class Display extends CI_Controller {
    public function __construct()
  {
        parent::__construct();
  }
    public function home()
    {
        // some codes here 
        saveVisit('home');
    }
}

I want saveVisit(); 我想要saveVisit(); called only in home page not others 仅在首页中调用,其他人未调用

EDITED : 编辑:

I created a new function in helper to test and called in default controller , function is called once BUT runned twice ! 我在助手中创建了一个新函数进行测试,并在默认控制器中调用该函数, 但是一旦运行两次该函数就会被调用!

test method code in helper : 助手中的测试方法代码:

 function test()
  {
      $CI =& get_instance();

      if ($CI->session->userdata('visit')!=null) {
        $plus = $CI->session->userdata('visit');
        $CI->session->set_userdata('visit',$plus+1);
        echo $CI->session->userdata('visit');
      }else {
        $CI->session->set_userdata('visit',1);
      }

And when i call test(); 当我调用test(); inside default Controller result for every page refresh is (Other controllers was Normal) : 在内部,默认情况下,每页刷新的Controller结果为(其他控制器为Normal):

2
4
6
8
10
12
14
.
.
. 

Why this happen ? 为什么会这样呢?

EDIT 3 : 编辑3:

When i call a helper function inside default controller it will run twice and run once from other controllers ! 当我在默认控制器内调用辅助函数时 ,它将运行两次,并从其他控制器运行一次! the problem is i didn't call function in other controllers 问题是我没有在其他控制器中调用函数

Instead of calling test() function to each of the function in Display class, call it inside __construct() 不用在Display类中的每个函数上调用test()函数,而是在__construct()调用它

<?php

class Display extends CI_Controller {

    public function __construct(){
        parent::__construct();
        test();
    }

    public function home(){
        // some codes here 
        saveVisit('home');

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM