CI 코드

제목 controllers 하위폴더 생성
글쓴이 앤드그리고 작성시각 2009/10/01 11:03:02
댓글 : 1 추천 : 0 스크랩 : 0 조회수 : 23280   RSS
안녕하세요.
ci 묻고 답하기에
controllers 하위폴더 생성에 대해서 질문이 올라왔는데요, 거기에 댓글로 답변을 달았는데 여기에도 같이 올려요~


기본적으로 ci에서 controller에 하위폴더를 하나까지 만들 수 있는데요...
하위폴더 아래에 하위폴더를 더 추가하기 위한 방법이에요.

예를들면 ci 기본으로는
controllers/aaa.php 또는
controllers/zzz/bbb.php 까지 만들 수 있는데

controllers/zzz/yyy/xxx/ccc.php 이런식으로 하위 폴더를 더 만들 기 위한 방법입니다.

아래 코드처럼 코어 Router를 확장해서 MY_Router를 만들어서 라이브러리에 추가하면 됩니다.
코어하고 크게 달라진 점은 없고, 하위 폴더를 찾는 부분만 약간 수정을 했습니다.

class MY_Router extends CI_Router {
  
    /**
     * Validates the supplied segments.  Attempts to determine the path to
     * the controller.
     *
     * @access    private
     * @param    array
     * @return    array
     */    
    function _validate_request($segments)
    {
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
        {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$this->fetch_directory().$segments[0]))
        {        
            // Set the directory and remove it from the segment array
            $this->_append_directory($segments[0]);
            $segments = array_slice($segments, 1);
            
            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    return $this->_validate_request($segments);
                }
            }
            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.EXT))
                {
                    $this->directory = '';
                    return array();
                }
            
            }

            return $segments;
        }

        // Can't find the requested controller...
        show_404($segments[0]);
    }

    /**
     *  Append the directory name
     *
     * @access  public
     * @param   string
     * @return  void
     */ 
    function _append_directory($dir)
    {
        $this->directory .= $dir.'/';
    }
}

 다음글 장바구니 시험적용 및 버그 (3)
 이전글 debug용 헬퍼. (3)

댓글

ci세상 / 2009/10/01 22:13:20 / 추천 0
1.7.2에서도 정상작동합니다. 감사합니다.^^