| 제목 | SEO 관련 고민을 하다가 | ||
|---|---|---|---|
| 글쓴이 | 천재작곡가 | 작성시각 | 2015/11/06 10:25:49 | 
|  | |||
| Hook을 이용한 레이아웃 사용중 SEO를 더 간편하게 넣을수 없을까 고민하다가 라이브러리를 한번 만들어봤습니다. 아직 초짜의 실력이라, 좋은코딩은 아니지만 고수님들의 조언을 얻을수 있을까 하고 한번 올려봅니다. 감사합니다! ◎ appliction/config/site_config.php 
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**************************************************
 * 
 * SEO 를 위한 메타 태그 설정
 * 
 **************************************************/
$config['site_title'] = '싱투미!';
$config['site_keywords'] = '싱투미,Single';
$config['site_robot'] = 'index,follow';
◎ application/config/autoload.php 
$autoload['config'] = array('site_config');◎ application/libraries/Seo.php 
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Seo {
	
	private $title 		= NULL;	// <title>$title</title>
	private $desc 		= NULL;	// <meta name="description" contents="$desc">
	private $keywords 	= NULL;
	private $charset 	= NULL;
	private $robots		= NULL;
	private $og_title 	= NULL;
	private $og_type 	= NULL;
	private $og_img		= NULL;
	private $og_url		= NULL;
		
	/********************************************************************************************
	 * 생성자
	 *******************************************************************************************/
	function __construct($data = array()) {
		
		$CI =& get_instance();
		
		$this->title 	= $CI->config->item('site_title');
		$this->keywords = $CI->config->item('site_keywords');
		$this->charset 	= $CI->config->item('charset');
		$this->robots	= $CI->config->item('robots');
		
		$this->set_meta($data);
	}
	
	/********************************************************************************************
	 * 배열 설정을 메타로 한번에 설정한다.
	 *******************************************************************************************/
	public function set_meta($meta = array()) {
		
		foreach($meta as $key=>$val) {
			
			if($key === "title") {
				// 타이틀일 경우에만 사이트 타이틀을 붙여준다.
				$this->set_title($val);
			}
			else {
				$this->$key = $val;
			}
		}
	}
	
	
	/********************************************************************************************
	 * 페이지 타이틀을 변경합니다.
	 *******************************************************************************************/
	public function set_title($title) {
		$CI =& get_instance();
		$this->title = $title . ' - ' . $CI->config->item('site_title');
	}
	
	
	/********************************************************************************************
	 * 현재 설정값을 이용하여 세팅된 Meta 태그값을 내보냅니다.
	 * 
	 * @return String $output 세팅된 Meta 태그
	 *******************************************************************************************/
	public function output(){
		
		$output = "<meta chaset='{$this->charset}'>"
				. "<title>{$this->title}</title>"
				. '<meta http-equiv="X-UA-Compatible" content="IE=edge">'
				. '<meta name="viewport" content="width=device-width, initial-scale=1">';
		
		if( $this->keywords ) {
			$output .= "<meta name='keywords' content='{$this->keywords}' />";
		}
		if( $this->desc ) {
			$output .= "<meta name='description' content='{$this->desc}' />";
		}
		
		if( $this->robots ) {
			$output .=  "<meta name='robots' content='{$this->robots}' />";
		}
		
		if( $this->og_title) {
			$output .= "<meta property='og:title' content='{$this->og_title}' />";
		}
		
		if( $this->og_type) {
			$output .= "<meta property='og:type' content='{$this->og_type}' />";
		} 
		
		if( $this->og_img) {
			$output .= "<meta property='og:image' content='{$this->og_img}' />";
		}
		
		if( $this->og_url) {
			$output .= "<meta property='og:url' content='{$this->og_url}' />";
		}
		
		return $output;
	}
	
	/********************************************************************************************
	 * 소멸자
	 *******************************************************************************************/
	function __destruct() {
		
	}
}	◎ 각 컨트롤러 
       /**************************************************
       * 현재 페이지의 SEO관련 세팅을 실행한다. (required)
     	**************************************************/
     	$meta = array(
     		"title" => "메인페이지"
		);
		$this->seo->set_meta($meta);◎ 뷰<!DOCTYPE html> <html lang="ko"> <head><?php echo $this->seo->output();?></head> | |||
| 태그 | SEO | ||
| 다음글 | CI 3.0.3 base_url(), site_url(... (2) | ||
| 이전글 | ie에서 발생하는 SESSION 오류 (3) | ||
| 
                                한대승(불의회상)
                                /
                                2015/11/06 13:18:38 /
                                추천
                                0
                             | 
| 
                                변종원(웅파)
                                /
                                2015/11/06 18:01:14 /
                                추천
                                0
                             
                                포럼소스에서는 뷰에만 해당 메타태그가 삽입되도록 작업 되어 있는데 hook으로 전역적으로 제어하는 것도 좋네요. ^^
                             | 
| 
                                스카이
                                /
                                2015/11/30 23:23:02 /
                                추천
                                0
                             
                                저도 SEO 작업에 관심이 많습니다. 참고하겠습니다. | 
소중한 자료 잘 사용 하겠습니다.