用户
 找回密码
 入住 CI 中国社区
搜索
查看: 9500|回复: 11
收起左侧

[HELP] controller中的A函数,可以调用controller中的B函数么?

  [复制链接]
发表于 2011-12-4 11:22:43 | 显示全部楼层 |阅读模式
各位版友好!

代码:
PHP复制代码
 
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
         */

 
        function __construct(){
                parent::__construct();
                $this->load->helper('url');
                $this->load->helper('form');
 
        }
        public function index()
        {
//              $this->load->view('index');
                $this->load->view('home');
        }
 
 
 
        public function sinaWburl2ID($url) {
                $surl[2]= str62to10(substr($url,strlen($url)-4,4));
                $surl[1]= str62to10(substr($url,strlen($url)-8,4));
                $surl[0]= str62to10(substr($url,0,strlen($url)-8));
                return $surl[0] . $surl[1] . $surl[2];
        }
 
        public function str62to10($str62){ //62进制到10进制
                $strarry=str_split($str62);
                $str=0;
            for ($i =0; $i <strlen($str62);$i++) {
                        $vi=Pow(62, (strlen($str62) - $i - 1));
                         $str+=$vi*str62keys($strarry[$i]);
                }
                 return $str;
        }
 
        public function str62keys($ks)//62进制字典
        {
                 $str62keys = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9","a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
                 return array_search($ks,$str62keys);
        }
 
 
        public function getCommentByUrl()
        {
                $url=$this->input->post('iurl');
               
                $id=sinaWburl2ID($url);
//              $id=3386232065026453;
 
               
               
 
                $data['mid']=$id;
 
                $this->load->view('test',$data);
 
 
        }
 
复制代码



我的程序是部署在SAE上面的。
结果一直报错:
SAE_Fatal_error: Call to undefined functionsinaWburl2ID()

我的疑问是:难道controller中的方法,不能互相调用么?
发表于 2011-12-4 11:49:44 | 显示全部楼层
当然不能啦,看看ci的运行原理吧
 楼主| 发表于 2011-12-4 11:53:18 | 显示全部楼层
jeongee 发表于 2011-12-4 11:49
当然不能啦,看看ci的运行原理吧

饿。。那你就告诉我,应该怎么做呢?
我现在自己的思路是:
   另外写一个 WeiBoUtility类,包含了这几个函数。
  然后在getCommentByUrl()函数中实例化这个类,再调用里面的函数,这样可以的吧?
发表于 2011-12-4 12:03:58 | 显示全部楼层
zhulicong 发表于 2011-12-4 11:53
饿。。那你就告诉我,应该怎么做呢?
我现在自己的思路是:
   另外写一个 WeiBoUtility类,包含了这几个 ...

这个思路可行
发表于 2011-12-4 13:11:39 | 显示全部楼层
你直接写在model里不行吗?干嘛非的死缠在controller里
 楼主| 发表于 2011-12-4 14:05:47 | 显示全部楼层
╰↓①秒つ. 发表于 2011-12-4 13:11
你直接写在model里不行吗?干嘛非的死缠在controller里

模型原来可以这么用啊?
我还以为模型只能用作与数据库打交道。。。
发表于 2011-12-4 17:23:47 | 显示全部楼层
我勒个去,php调用类内部的方法要用 $this->sinaWburl2ID()
发表于 2011-12-5 23:58:01 | 显示全部楼层
7楼正解,我测试是可以的。
PHP复制代码
 
public function index()
        {
                $this->zjm();
                $this->load->view('welcome_message');
        }
       
        public function zjm($p1 = 'a', $p2 = 'b')
        {
                print_r($_GET);
                echo "$p1<br />$p2";
                echo "cc";
        }
 
复制代码
[/quote]
发表于 2011-12-6 09:26:22 | 显示全部楼层
7楼正解,不过不建议controller里面互相调用,把要调用的放到model 层。方便别的方法来调用。
发表于 2011-12-6 09:42:32 | 显示全部楼层
轻抚菊花笑不语,终于解开这个困扰了

本版积分规则