3

サイト全体で利用できる機能を含むカスタム ライブラリを構築しようとしています。内部/application/librariesで新しいファイルを作成しましたCustomFuncts

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @brief CommonFuncts      
 * This class is used to provide common user functions to the whole application.
 *
 * @version 1.0
 * @date May 2012
 *
 * 
 */
class CommonFuncts extends CI_Controller {

 protected $ci;

/**
 * function constructor
 */
function __construct()
{
    $this->ci =& get_instance();
}
 /**
* @brief checkCookie
* 
* Checks for a previous cookie, and if exists:
* @li Loads user details to CI Session object.
* @li Redirects to the corresponding page.
* 
*/
public function verificaCookie(){
    $this->ci->load->view('index');
}
 }
/* End of file CommonFuncts.php */
/* Location: ./application/libraries/CommonFuncts.php */

そしてコントローラーでwelcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/welcome
 *  - or -  
 *      http://example.com/index.php/welcome/index
 *  - or -
 * Since this controller is set as the default controller in 
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see http://codeigniter.com/user_guide/general/urls.html
 */
public function index()
{
    $this->load->library('commonFuncts');
    $this->commonFuncts->verificaCookie();  
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

次のエラー メッセージが表示されます。

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$commonFuncts

Filename: controllers/welcome.php

Line Number: 23

Fatal error: Call to a member function verificaCookie() on a non-object in
/var/www/vhosts/unikodf.com/httpdocs/application/controllers/welcome.php on line 23

CI_Controllerライブラリの拡張や $this->ci->load->view` の使用など、多くのことを試しまし$this->load->view' instead ofたが、それでも同じメッセージが表示されます。

4

5 に答える 5

10

ライブラリが拡張する場合CI_Controller。これは、コントローラーの機能を実際に拡張していることを意味します。代わりに、次のようにライブラリを宣言します。

class CommonFuncts { }

CI_Controllerコントローラーではなくライブラリーを作成しており、ライブラリーは MVC モデルの一部ではないため、 から継承する必要はありません。フレームワークの機能を拡張したり、一般的な問題を修正したり、多くのコントローラーで使用される機能を備えたりする単一のクラスです。

そのメソッドにアクセスするには、次を使用します。

$this->load->library('CommonFuncts');
$this->commonfuncts->verificaCookie();

ライブラリを呼び出す名前を単純化したい場合は、次のようにします。

// The second argument is the optional configuration passed to the library
// The third argument is the name with which you would like to access it
$this->load->library('CommonFuncts', '', 'common');
$this->common->verificaCookie(); //  /\ = configuration
//     /\/\/\ = name
于 2012-05-19T06:11:28.977 に答える
4

コントローラライブラリを混同しています。

カスタム ライブラリはCI_Controller を拡張しないでください。このドキュメントでは、独自のカスタム ライブラリを作成する方法について概説しています

次に、メソッドにアクセスする前にライブラリをロードする必要があります。

$this->load->library('CommonFuncts');
// The "CommonFuncts" library is now ready to use
$this->commonFuncts->verificaCookie(); 
于 2012-05-18T16:57:21.303 に答える
0

ここで、1 つのことを変更します (コード イグナイター 2.1.3 を使用しています)。

// CI_VERSION をエコーし​​ます。コードイグナイタのバージョンを取得するには
ご利用ください
$this->commonfuncts->verificaCookie(); //f はここでは小さい commonfuncts
それ以外の
$this->commonFuncts->verificaCookie();
于 2014-11-16T13:26:39.437 に答える