2013-07-04 3 views
3

로드, 내가하지 :CodeIgniter는 내 컨트롤러 (컨트롤러/user.php)에서 모델

내 모델 (모델/user_model.php)에서
class User extends CI_Controller 
{ 
    public function index() 
    {  
     $this->load->model('user_model')or die("error"); 
    } 
} 

, 나는

class User_model extends CI_Model 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->database(); 
     $this->load->helper('common'); 
    } 
} 

만약이 I

or die("error"); 

로드 명령문에서 500 개의 내부 서버 오류가 발생합니다.

여기 config.php를 확인하고는 나는 또한 청소기를 만들기 위해 URL에서 "index.php에"를 제거 할 수있는 htaccess로 파일을 편집 한 몇 가지 정보

$config['base_url'] = ''; 
$config['index_page'] = ''; 
$config['uri_protocol'] = 'AUTO'; 

에게 있습니다. 그것은 말한다

RewriteEngine On 
RewriteCond $1 !^(index.php|images|captcha|css|js|robots.txt) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 
+0

로컬 호스트 또는 테스트/라이브 사이트를 온라인 상태로 만들었습니까? – sinisake

+0

지금 당장 로컬에서 테스트하고 있습니다. MAMP – Lance

+0

'$ this-> load-> model ('user_model') 또는 die ("error")를 제거하고 echo 'a'; 출구; 'a'를 주면 문제가 모델과 관련됩니다. application/config/database.php에서 구성을 확인하려면 다음 단계로해야합니다. $ this-> load-> database(); (도우미를로드하기 전에) 데이터베이스 연결인지 확인하십시오. – galchen

답변

3

모델을로드하는 것이 가장 좋습니다, __construct 도서관 (생성자)

class User extends CI_Controller 
{ 
    public function __construct() 
    {  
     parent:: __construct(); 
     $this->load->model('user_model'); 
    } 

} 할 수

또한 '사용자'에 컨트롤러 이름 '사용자'변경 시도하십시오 (잘못하지는 않았지만 작동하지 않으면 시도하십시오). 명명 충돌이있을 수 있습니다. 당신 만 대신의 기능에서 모델을로드에만 특정 기능에 대한 모델을로드해야하는 경우

# in the User controller 
class User extends CI_Controller{ 
    public function __construct(){ 
     parent::__construct(); 
     $this->load->database(); 
     $this->load->helper('common'); 
     $this->load->model('user_model'); 
    } 

    public function index() 
    {  
     //$this->load->model('user_model')or die("error"); 
     #now you can use the user_model here 
    } 
} 

:

+1

아직도 작동하지 않습니다. – Lance

0
class User extends CI_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->database(); 
     $this->load->helper('common'); 
     $this->load->model('user_model'); 
    }  

    public function index() 
    {  

    } 
} 
+0

아직도 작동하지 않습니다. – Lance

0

망가 대신에이 규칙을 따르지 모델의 contruct 방법을 사용 건설자. 생성자에 모델을로드하면 모델 함수를 모든 컨트롤러 함수에서 사용할 수 있습니다.

+0

아직도 작동하지 않습니다. – Lance

0

그것은 또한 당신이 모델을로드 할 때 자동으로 데이터베이스에 연결하지는 않지만 다음 세 번째 매개 변수로 TRUE 전달하는 경우가

class User extends CI_Controller 
{ 
    public function __construct() 
    {  
     parent:: __construct(); 
     $this->load->model('user_model', '', TRUE); 
    } 
} 

당신은 당신이가는 알 수있는 경우 협조 할 것입니다 모델을 많이로드하는 중 application/config/autoload.php 파일에 자동로드 할 수도 있습니다.

+0

아직도 작동하지 않습니다. – Lance

-1

파일 이름을 확인하십시오. 확장자가 .php입니까 ?? 나 자신이이 문제가있어서 .php 확장자를 추가하는 것을 잊어 버렸다.