| 제목 | 컨트롤러에서 모델 인스턴스를 동적으로 로드하고싶습니다 | ||
|---|---|---|---|
| 카테고리 | CI 4 관련 | ||
| 글쓴이 | dg_dong | 작성시각 | 2022/01/19 17:52:26 | 
|  | |||
| 접속 URL EX) http://localhost/user/top http://localhost/user/center http://localhost/user/bottom 
 Routes.php 
EX) 
$routes->group('user',function($routes){    
    $routes->get('(:segment)'   ,'Api\User::search/$1');}
);접속한 segment를 활용해 new TopModel , new CenterModel ,new BottomModel 모델 인스턴스를 동적으로 만들수있는 방법이 있을까요? Top , Center , Bottom 이 부분을 segment값으로 대체하고 싶습니다 
 현재는 switch문으로 분기해서 작업 했습니다 
switch($cd){
            case 'Top' : 
                $model = new TopModel;
                break;
            case 'Center' :
                $model = new CenterModel;
                break;
            case 'Bottom' : 
                $model = new BottonModel;
                break;
        }
        return $this->setResponseFormat('json')->respond([$model->findAll()]);
 | |||
| 다음글 | Docker기반 환경에서 CI4와 DB 접속이 안되는 ... (2) | ||
| 이전글 | ci zip 문의 (1) | ||
| 
                                츠츤츤
                                /
                                2022/01/20 10:03:20 /
                                추천
                                0
                             
                                저도 궁금합니다. 그리고 ci3에서는 모델에 함수를 만들고 값을 넘겨 분기처리를 하였는데요. ci4에선 db table 별로 모델클래스를 새로 만들고 컨트롤 단에서 분기처리해야하는지 궁금합니다.
                             | 
| 
                                한대승(불의회상)
                                /
                                2022/01/20 11:11:15 /
                                추천
                                0
                             $modelName = $cd.'Model'; $model = new $modelName; | 
| 
                                테러보이
                                /
                                2022/01/20 13:15:16 /
                                추천
                                0
                             가정 윗줄에 use로 모델은 불러오셨을거고 그렇다면 다음과 같은 코드가 되야겠지요. 
switch($cd){
    case 'top' :
        $model = new TopModel();
        break;
    case 'center' :
        $model = new CenterModel();
        break;
    case 'bottom' :
        $model = new BottonModel();
        break;
}
return $this->setResponseFormat('json')->respond([$model->findAll()]);
 |