기본 콘텐츠로 건너뛰기

[PHP] CodeIgniter 2단계 이상의 하위 폴더로 소스 관리하는 방법

[PHP] CodeIgniter 2단계 이상의 하위 폴더로 소스 관리하는 방법

개요

CodeIgniter를 이용하여 REST API를 개발하던 중에 다음과 같은 형태로 폴더 구조를 만들고, API를 호출하면 무조건 "404 Not found" 오류를 반환하였습니다.

application / controllers / rapi / v1.0 / Welcome.php application / controllers / rapi / Welcome.php

curl -XGET "http://localhost/rapi/v1.0/Welcome

동일한 "Welcome.php" 소스를 "rapi" 폴더에 복사하고 다음과 같이 호출하면 정상적으로 호출됩니다.

curl -XGET "http://localhost/rapi/Welcome

검색하여 보니 기본적으로 CodeIgniter를 이용하여 controllers 폴더 하위에 폴더를 생성하여 각 폴더별로 소스를 관리하는 것이 가능하기는 하지만, 1단계 하위 폴더까지만 인식이 되고 그 하위 폴더들은 인식이 되지 않고 무조건 404 오류가 반환되도록 되어 있는 것이었습니다.

1단계 하위 폴더 아래 추가로 하위 폴더를 더 만들어서 소스들을 관리하기 위해서는 CodeIgniter의 기본 Router 클래스를 상속받아 확장해야만 가능합니다.

작업 내용

2단계가 넘어가는 하위 폴더별로 프로젝트 소스를 분리하여 관리하기 위해서는 다음의 "MY_Router.php" 소스를 "application/core" 폴더 아래에 복사합니다.

0 ) { $dir .= '/' ; } $dir .= $segments [ 0 ]; $segments = array_slice ( $segments , 1 ); } while ( count ( $segments ) > 0 && is_dir ( APPPATH . 'controllers/' . $dir . '/' . $segments [ 0 ])); // Set the directory and remove it from the segment array $this -> set_directory ( $dir ); // @edit: END // @edit: If no controller found, use 'default_controller' as defined in 'config/routes.php' if ( count ( $segments ) > 0 && ! file_exists ( APPPATH . 'controllers/' . $this -> fetch_directory () . $segments [ 0 ] . '.php' )) { array_unshift ( $segments , $this -> default_controller ); } // @edit: END if ( count ( $segments ) > 0 ) { // Does the requested controller exist in the sub-folder? if ( ! file_exists ( APPPATH . 'controllers/' . $this -> fetch_directory () . $segments [ 0 ] . '.php' )) { // show_404($this->fetch_directory().$segments[0]); // @edit: Fix a "bug" where show_404 is called before all the core classes are loaded $this -> directory = '' ; // @edit: END } } else { // Is the method being specified in the route? if ( strpos ( $this -> default_controller , '/' ) !== FALSE ) { $x = explode ( '/' , $this -> default_controller ); $this -> set_class ( $x [ 0 ]); $this -> set_method ( $x [ 1 ]); } else { $this -> set_class ( $this -> default_controller ); $this -> set_method ( 'index' ); } // Does the default controller exist in the sub-folder? if ( ! file_exists ( APPPATH . 'controllers/' . $this -> fetch_directory () . $this -> default_controller . '.php' )) { $this -> directory = '' ; return array (); } } return $segments ; } // If we've gotten this far it means that the URI does not correlate to a valid // controller class. We will now see if there is an override if ( ! empty ( $this -> routes [ '404_override' ])) { $x = explode ( '/' , $this -> routes [ '404_override' ]); $this -> set_class ( $x [ 0 ]); $this -> set_method ( isset ( $x [ 1 ]) ? $x [ 1 ] : 'index' ); return $x ; } // Nothing else to do at this point but show a 404 show_404 ( $segments [ 0 ]); } // -------------------------------------------------------------------- /** * OVERRIDE * * Set the directory name * * @access public * @param string * @return void */ function set_directory ( $dir , $append = false ) { $this -> directory = $dir . '/' ; // @edit: Preserve '/' } // -------------------------------------------------------------------- }

결론

CodeIgniter에서는 기본적으로 controllers 폴더 아래 1단계 하위에 폴더까지만 각 폴더별로 소스를 관리하는 것이 가능하고, 1단계 하위 폴더 아래 추가로 하위 폴더를 더 만들어서 소스들을 관리하기 위해서는 CodeIgniter의 기본 Router 클래스를 상속받아 확장해야만 가능합니다.

2단계가 넘어가는 하위 폴더별로 프로젝트 소스를 분리하여 관리하기 위해서는 본문의 "MY_Router.php" 소스를 "application/core" 폴더 아래에 복사해 두면, 서브 폴더를 다단계로 나누어 소스의 관리가 가능합니다.

참고자료

from http://hbesthee.tistory.com/1633 by ccl(A)

댓글

이 블로그의 인기 게시물

[PHP] 코드이그니터 - 파일업로드 구현

[PHP] 코드이그니터 - 파일업로드 구현 파일 업로드 이번에 PHP 프레임워크인 코드 이그니터(Codeigniter)를 사용하여 홈페이지를 만드는데 사용한 이미지 업로드용 코드 입니다. upload 라이브러리를 사용하고 app~ 와 같은 위치에 upload 폴더를 만드고 다음 코드를 사용한다음 ajax 로 호출하여 파일을 업로드 합니다. function index() { // Upload 설정 $config['upload_path'] = './upload/'; $config[\'allowed_types\'] = 'gif|jpg|png'; $config['max_size'] = 100; // 100k $config['max_width'] = 1024; $config['max_height'] = 768; $this->load->library('upload', $config); $data = array(); if (! $this->upload->do_upload("service_image")) { $error = array('error' => $this->upload->display_errors()); } else { //$data = array('upload_data' => $this->upload->data()); $this->output->set_output("./upload/".$this->upload->data('file_name')); } } jquery 를 이용한 파일 업로드 호출 코드 function upload() { var datas, xhr; datas = new FormData(); datas.append( 'service_image', $( ...

2017년 1월 스타트업에서 구인할때 주로 원하는 개발 기술

2017년 1월 스타트업에서 구인할때 주로 원하는 개발 기술 php mysql linux android git kotlin gcm/fcm python mssql mongodb amazon aws ios objective-c swift github python c++ django python postgresql amazon aws html5/css3/javascript android java mysql python c++ c# java aws cloud-server dbms node.js postgresql redis nginx react.js hapi.js amazon aws restful-api angularJS jQuery html5/css3/javascript android firebase custom ui component restful-api asp.net c# html css javascript bootstrap angularjs node.js php mongodb redis 프론트엔드 주요 기술 javascript jquery ajax angularjs wbesocket html5/css3/javascript android ios java xcode node.js coffeescript mysql amazon ec2 amazon es3 android ios node.js php python java ios php mysql apache python android redis node.js jquery msa node.js java restful-api linux nosql golang redis nginx ...

이클립스 코드이그나이터 연동 ( eclipse codeigniter )

이클립스 코드이그나이터 연동 ( eclipse codeigniter ) https://ellislab.com/codeigniter/user-guide/installation/downloads.html 위의 사이트에서 코드이그나이터를 다운 받는다. 다운받은 압축 파일을 풀어 준다. 이클립스에서 php 프로젝트를 생성한 공간에 코드이그나이터 압축파일을 복사 붙여넣기 해준다. 위와 같은 화면이 나오면 정상적으로 연동이 된 것 입니다. from http://nahosung.tistory.com/22 by ccl(A) rewrite - 2020-03-06 16:20:55