|
楼主 |
发表于 2017-3-15 15:43:05
|
显示全部楼层
问题一:
××××××××××××××××××××××××××××××
<?php
defined('BASEPATH') OR 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 https://codeigniter.com/user_guide/general/urls.html
*/
public function index(){
/* $nji = $this->load->library('testclass');
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'kiu' => $nji
);
$this -> load ->view('blogview.html',$data);*/
$this -> load->library('testclass');
$this ->testclass->loadfun();
}
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 这个类是我写在 libraries 里面的一个脚本文件,
* 我想在 view 层里面 调用这个loadfun() 方法 .
* 显示 echo 所输出的内容,,,
*/
class Testclass{
protected $CI;
public function _construct() {
$this -> $CI =& get_instance();
}
public function testlass() {
echo $this ->loadfun();
}
/*
* 此注释 只针对列出的问题一, 使用return 不会显示任何内容 echo却能显示的,,,
* */
public function loadfun() {
return "通过 this 加载数据";
}
}
***********************************************************************************************
问题二 在view 调用,还是不行,,,,
<?php
defined('BASEPATH') OR 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 https://codeigniter.com/user_guide/general/urls.html
*/
public function index(){
$nji = $this->load->library('testclass');
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'kiu' => $nji
);
$this -> load ->view('blogview.html',$data);
/* $this -> load->library('testclass');
$this ->testclass->loadfun();*/
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1> 这是一个 view </h1>
<!--
错误信息: Type: Error
Message: Call to undefined method CI_Loader::loadfun()
-->
<!-- <?php
echo $kiu ->loadfun();
?>-->
<!--
或是另一种写法, 没有任何数据输出
-->
<?php
echo $nji ->loadfun();
?>
</body>
</html>
|
|