CodeIgniter自定义库未加载 [英] CodeIgniter Custom Library Not Loading

查看:138
本文介绍了CodeIgniter自定义库未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚接触CodeIgniter,试图用它开发一个相当简单的应用程序 - 只是一个表单来处理需要娱乐中心通行证的员工的注册。我试图分离的东西,使它们更清洁。



以下是代码:



application / controllers / reccenter.php

  class RecCenter extends CI_Controller {

public function register(){
$ data ['title'] =Utah Valley注册中心;

$ this-> output-> enable_profiler(TRUE); //将所有调试语句放在页面底部
$ this-> load-> helper('html');

$ this-> load-> library('form_validation');
$ this-> form_validation-> set_rules('userRecCenter','娱乐中心选择','必需');

$ userRecCenter = $ this-> input-> post('userRecCenter');
$ registerSelf = $ this-> input-> post('registerSelf');
$ dependents = $ this-> input-> post('dependents');

if($ this-> input-> post('registerSelf')&&!$ registerSelf){//员工未注册自己
$ this-> form_validation - > set_rules('dependents','self selection or dependents','required');
} else if($ this-> input-> post('dependents')&&&&&count($ dependents)== 1&& $ dependents [0] == // Employee only registered themselves
$ this-> form_validation-> set_rules('registerSelf','self selection or dependents','required');
}

if($ this-> form_validation-> run()== FALSE){
$ this-> load-& header',$ data);
$ this-> load-> view('pages / reccenter / register',$ data);
$ this-> load-> view('templates / footer',$ data);
} else {
$ this-> load-> library('Reccenterdao');
$ numRows = $ this-> reccenterdao-> getRecCenterInfo();
var_dump($ numRows);

//检查是否已经注册
//重定向到不同的视图以更改注册

//注册
// redirect(' reccenter / confirm');
}
}

public function confirm(){
$ data ['title'] =犹他谷注册确认;

$ this-> load-> view('templates / header',$ data);
$ this-> load-> view('pages / reccenter / confirm',$ data);
$ this-> load-> view('templates / footer',$ data);
}
}

application / libraries / Reccenterdao.php

  if(!defined('BASEPATH'))exit('不允许直接脚本访问); 

class Reccenterdao {

private $ CI;

public function __construct(){
$ this-> CI =& get_instance();
}

私人函数connect(){
$ this-> CI-> load-> database
}

私有函数disconnect(){
$ this-> CI-> db-> close();
}

public function getRecCenterInfo(){
$ this-> connect();

var_dump(Made it Here);
$ result = $ CI-> db-> query(SELECT * FROM ucfitness);
var_dump($ result-> result());

$ this-> disconnect();
return $ result-> num_rows();
}


}



application / config / autoload.php

  $ autoload ['helper'] = array('form','url'); 

webroot / index.php(适用于环境定义)

  if(isset($ _ SERVER ['HTTP_HOST'])&& $ _SERVER ['HTTP_HOST'] ==my prod server){
define ('ENVIRONMENT','prod');
} else if(isset($ _ SERVER ['HTTP_HOST'])&& $ _SERVER ['HTTP_HOST'] ==my stage server){
define('ENVIRONMENT','stage ');
} else {
define('ENVIRONMENT','test');
}

if(defined('ENVIRONMENT'))
{
switch(ENVIRONMENT)
{
case'test':
error_reporting(E_ALL);
break;

case'stage':
case'prod':
error_reporting(0);
break;

默认值:
exit('应用程序环境设置不正确。
}
}

我也复制了 application / config / database.php 分成三个文件夹, application / config / test / database.php /stage/database.php application / config / prod / database.php ,以配置其各自的连接参数。每个环境都有不同的数据库服务器和凭据。



问题是,当我提交表单,它使它在窗体验证后的else语句 $ this-> form_validation-> run()我只得到一个空白屏幕。没有我绝望的 var_dump s,没什么。所以,在我的所有尝试和不同的方法,使这项工作后,一些问题出现了。


  1. 什么是不工作在图书馆?通过所有的教程/问题,我可以找到,它看起来像我做的正确。

  2. 如何使它,以便我可以实际调试代码?我在浏览器中没有错误,没有错误在Apache错误日志。我还能做什么?

  3. 将DAO与控制器分开最好的做法是什么?从我可以告诉,使这个图书馆像我试图是最好的主意。它可能只由这一个控制器使用,它将处理注册的注册和修改。我将编写几个其他应用程序,它们都将位于此CI实例中,但它们将是相当独立的,不需要使用相同的DAO。

  4. 我可以使用CamelCase作为库文件名,只要它以大写字母开头? Reccenterdao.php RecCenterDAO.php 不太友好。

=========================== =============================



编辑:我改变了整个事情到一个模型,但我得到相同的功能 - 空白屏幕没有错误。它似乎加载模型,因为它尝试实际使用它与 $ numRows = $ this-> dao-> getRecCenterInfo();



我知道整个事情是毫无意义的,因为我没有使用数据库或解析用户输入的功能。这是整个问题。



应用程序/ models / rec_center_dao.php

$ b我在尝试使用所有的业务逻辑
$ b> class Rec_center_dao extends CI_Model {

public function __construct(){
parent :: __ construct
}

私人函数connect(){
$ this-> load-> database();
}

私有函数disconnect(){
$ this-> db-> close();
}

public function getRecCenterInfo(){
$ this-> connect();

var_dump(Made it Here);
$ result = $ this-> db-> query(SELECT * FROM ucfitness);
var_dump($ result-> result());

$ this-> disconnect();
return $ result-> num_rows();
}
}

application / controllers / reccenter.php的相关部分

  if($ this-> form_validation-> run()== FALSE){
$ this-> ; load-> view('templates / header',$ data);
$ this-> load-> view('pages / reccenter / register',$ data);
$ this-> load-> view('templates / footer',$ data);
} else {
$ this-> load-> model('Rec_center_dao','dao');
$ numRows = $ this-> dao-> getRecCenterInfo(); //这是它死的地方
var_dump($ numRows);

//检查是否已经注册
//重定向到不同的视图以更改注册

//注册
// redirect(' reccenter / confirm');
}

================ ============================================

EDIT2:感谢评论,我已经想出这是一个连接到数据库的问题。当我调用 $ this-> load-> database(); 时会出错。我已经确认,凭借我有我可以用phpMyAdmin管理数据库(我没有shell访问测试那里)。



application / config / test / database.php

  $ active_group ='default'; 
$ active_record = TRUE;

$ db ['default'] ['hostname'] ='my.hostname.com';
$ db ['default'] ['username'] ='db_username';
$ db ['default'] ['password'] ='db_password';
$ db ['default'] ['database'] ='db'
$ db ['default'] ['dbdriver'] ='mysql';
$ db ['default'] ['dbprefix'] ='';
$ db ['default'] ['pconnect'] = TRUE;
$ db ['default'] ['db_debug'] = TRUE;
$ db ['default'] ['cache_on'] = FALSE;
$ db ['default'] ['cachedir'] ='';
$ db ['default'] ['char_set'] ='utf8';
$ db ['default'] ['dbcollat​​'] ='utf8_general_ci';
$ db ['default'] ['swap_pre'] ='';
$ db ['default'] ['autoinit'] = TRUE;
$ db ['default'] ['stricton'] = TRUE;

使用上述配置 webroot / index.php application / config / test / database.php 它应该选择不同环境的设置,并相应地处理,正确?但是如果我在 application / config / database.php 应用程序中放置 var_dump config / test / database.php 它根本不显示。我确认环境设置正确(至少在测试时)。

解决方案

解决问题是我的PHP安装。我运行Windows 7和使用MSI安装程序来安装PHP 5.2.14。我使用 php-5.2.14-Win32-VC6-x86.zip 重做安装。解压缩它,把它放到位,重新配置php.ini,它工作。



不要使用MSI安装程序!


I'm new to CodeIgniter and trying to developing a fairly simple application with it -- just a form to process the registration for employees wanting a recreation center pass. I'm trying to separate things to make them cleaner.

Here is the code:

application/controllers/reccenter.php

class RecCenter extends CI_Controller {

    public function register() {
        $data['title'] = "Utah Valley Rec Center Registration";

        $this->output->enable_profiler(TRUE); // Put all the debug statements at the bottom of the page
        $this->load->helper('html');

        $this->load->library('form_validation');
        $this->form_validation->set_rules('userRecCenter', 'recreation center selection', 'required');

        $userRecCenter = $this->input->post('userRecCenter');
        $registerSelf = $this->input->post('registerSelf');
        $dependents = $this->input->post('dependents');

        if($this->input->post('registerSelf') && !$registerSelf) { // Employee not registering themselves
            $this->form_validation->set_rules('dependents', 'self selection or dependents', 'required');
        } else if($this->input->post('dependents') && count($dependents) == 1 && $dependents[0] == "") { // Employee only registering themselves
            $this->form_validation->set_rules('registerSelf', 'self selection or dependents', 'required');
        }

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('pages/reccenter/register', $data);
            $this->load->view('templates/footer', $data);
        } else {
            $this->load->library('Reccenterdao');
            $numRows = $this->reccenterdao->getRecCenterInfo();
            var_dump($numRows);

            // Check if already registered 
                // Redirect to different view to change their registration

            // Do the registration
                //redirect('reccenter/confirm');
        }
    }

    public function confirm() {
        $data['title'] = "Utah Valley Rec Center Registration Confirmation";

        $this->load->view('templates/header', $data);
        $this->load->view('pages/reccenter/confirm', $data);
        $this->load->view('templates/footer', $data);
    }
}

application/libraries/Reccenterdao.php

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

class Reccenterdao {

    private $CI;

    public function __construct() { 
        $this->CI =& get_instance();
    }

    private function connect() {
        $this->CI->load->database();
    }

    private function disconnect() {
        $this->CI->db->close();
    }

    public function getRecCenterInfo() {
        $this->connect();

        var_dump("Made it here");
        $result = $CI->db->query("SELECT * FROM ucfitness");
        var_dump($result->result());

        $this->disconnect();
        return $result->num_rows();
    }


}

Here are a couple of additional configuration items, so you know what all I've done to customize it a bit.

application/config/autoload.php

$autoload['helper'] = array('form', 'url');

webroot/index.php (For environment definitions)

if(isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == "my prod server") {
    define('ENVIRONMENT', 'prod');  
} else if(isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == "my stage server") {
    define('ENVIRONMENT', 'stage'); 
} else {
    define('ENVIRONMENT', 'test');  
}

if (defined('ENVIRONMENT'))
{
    switch (ENVIRONMENT)
    {
        case 'test':
            error_reporting(E_ALL);
        break;

        case 'stage':
        case 'prod':
            error_reporting(0);
        break;

        default:
            exit('The application environment is not set correctly.');
    }
}

I've also copied application/config/database.php into three folders, application/config/test/database.php, application/config/stage/database.php, and application/config/prod/database.php, to configure their individual connection parameters. Each environment has a different database server and credentials.

The problem is that when I submit the form and it makes it in to that else statement after the form validation $this->form_validation->run() I'm getting only a blank screen. None of my desperate var_dumps, nothing. So, after all of my attempts and different methods of getting this to work, a few questions have come up.

  1. What isn't working in the library? After going through all the tutorials/questions that I can find, it looks like I'm doing it right.
  2. How do I make it so that I can actually debug the code? I get no errors in the browser and no errors on the Apache error log. What else can I do?
  3. What would the best practice be for separating the DAO from the Controller? From what I could tell, making this a library like I'm trying would be the best idea. It is probably only going to be used by this one controller, which will handle both registration and modification of the registration. I will be writing a couple of other applications that will all sit within this CI instance, but they're going to be fairly separate and will not need to use the same DAO.
  4. Can I use CamelCase for the library file name as long as it starts with a capital letter? Reccenterdao.php is a lot less friendly than RecCenterDAO.php.

============================================================

EDIT: I changed the whole thing to a model, but I'm getting the same functionality - blank screen with no errors. It seems to load the model, because it dies when I try to actually use it with the line $numRows = $this->dao->getRecCenterInfo();

I know the whole thing is pointless right now because I haven't put functionality to use the database or parse the user input. That's the whole point. I was trying to get it to work before I went through the effort of putting in all my business logic.

application/models/rec_center_dao.php

class Rec_center_dao extends CI_Model {

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

    private function connect() {
        $this->load->database();
    }

    private function disconnect() {
        $this->db->close();
    }

    public function getRecCenterInfo() {
        $this->connect();

        var_dump("Made it here");
        $result = $this->db->query("SELECT * FROM ucfitness");
        var_dump($result->result());

        $this->disconnect();
        return $result->num_rows();
    }
}

Relevant section of application/controllers/reccenter.php

    if ($this->form_validation->run() == FALSE) {
        $this->load->view('templates/header', $data);
        $this->load->view('pages/reccenter/register', $data);
        $this->load->view('templates/footer', $data);
    } else {
        $this->load->model('Rec_center_dao', 'dao');
        $numRows = $this->dao->getRecCenterInfo(); // This is where it dies
        var_dump($numRows);

        // Check if already registered 
            // Redirect to different view to change their registration

        // Do the registration
            //redirect('reccenter/confirm');
    }

============================================================

EDIT2: Thanks to the comments, I've figured out that it's a problem connecting to the database. It errors out when I call $this->load->database();. I've confirmed that with the credentials I have I can manage the database with phpMyAdmin (I don't have shell access to test there).

application/config/test/database.php

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'my.hostname.com';
$db['default']['username'] = 'db_username';
$db['default']['password'] = 'db_password';
$db['default']['database'] = 'db'
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = TRUE;

With the above configurations in webroot/index.php and application/config/test/database.php it should pick up the settings for the different environments and take care of it accordingly, correct? But if I put a var_dump in either application/config/database.php or application/config/test/database.php it doesn't show up at all. I confirmed that the environment is being set correctly (at least while I'm working on test).

解决方案

Turns out the problem was my PHP installation. I'm running Windows 7 and used the MSI installer to install PHP 5.2.14. I used the php-5.2.14-Win32-VC6-x86.zip to redo the installation. Unzipped it, put it in place, reconfigured the php.ini, and it worked.

Don't use the MSI installers!

这篇关于CodeIgniter自定义库未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆