CodeIgniter에 머리글과 바닥글을 원활하게 통합
모든 컨트롤러에서 머리글과 바닥글 보기를 반복적으로 로드해야 하는 작업은 지루할 수 있습니다. 이 문제를 해결하기 위해 이 프로세스를 자동화하고 이러한 공통 요소를 사용자 정의할 수 있는 유연성을 제공하는 솔루션을 살펴보겠습니다.
CodeIgniter에서는 이를 달성하기 위해 사용자 정의 로더 클래스를 생성할 수 있습니다. MY_Loader.php의 template() 메소드를 재정의함으로써 헤더, 본문 및 바닥글 보기를 결합하는 함수를 정의할 수 있습니다.
<code class="php">// application/core/MY_Loader.php class MY_Loader extends CI_Loader { public function template($template_name, $vars = array(), $return = FALSE) { $content = $this->view('templates/header', $vars, $return); $content .= $this->view($template_name, $vars, $return); $content .= $this->view('templates/footer', $vars, $return); if ($return) { return $content; } } }</code>
CodeIgniter 3.x의 경우 수정된 template() 메소드는 추가 elseif 문 포함:
<code class="php">// application/core/MY_Loader.php class MY_Loader extends CI_Loader { public function template($template_name, $vars = array(), $return = FALSE) { if($return): $content = $this->view('templates/header', $vars, $return); $content .= $this->view($template_name, $vars, $return); $content .= $this->view('templates/footer', $vars, $return); return $content; elseif: $this->view('templates/header', $vars); $this->view($template_name, $vars); $this->view('templates/footer', $vars); endif; } }</code>
이 사용자 정의 로더를 사용하면 컨트롤러는 머리글 및 바닥글 보기에 대해 걱정하지 않고 원하는 본문 콘텐츠를 간단히 로드할 수 있습니다.
<code class="php">// controller $this->load->template('body');</code>
이 접근 방식은 유연성을 제공합니다. 컨트롤러를 복잡하게 하지 않고도 머리글과 바닥글 콘텐츠를 쉽게 사용자 정의할 수 있습니다.
위 내용은 CodeIgniter에서 머리글 및 바닥글 포함을 자동화하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!