77use Butschster \Head \MetaTags \Meta ;
88use Butschster \Head \MetaTags \Entities \JavascriptVariables ;
99use Butschster \Head \Packages \Entities \OpenGraphPackage ;
10+ use Butschster \Head \Packages \Entities \TwitterCardPackage ;
1011use Butschster \Head \Providers \MetaTagsApplicationServiceProvider as ServiceProvider ;
1112use Illuminate \Support \Facades \App ;
1213use Illuminate \Support \Facades \Config ;
1314use Illuminate \Support \Facades \File ;
1415use Illuminate \Support \Facades \Lang ;
1516use Illuminate \Support \Facades \Log ;
1617use Illuminate \Support \Facades \Route ;
18+ use Illuminate \Support \Str ;
19+ use OPGG \LaravelEssentialsEntry \Facades \EssentialsEntry as EssentialsEntryFacade ;
1720use OPGG \LaravelEssentialsEntry \Http \Middleware \DetectLanguage ;
1821
1922class MetaTagsServiceProvider extends ServiceProvider
@@ -31,17 +34,17 @@ protected function checkTranslationKey(string $key): void
3134 $ defaultLocale = Config::get ('essentials-entry.language.default ' , 'en ' );
3235 if (!Lang::has ($ key , $ defaultLocale )) {
3336 $ langPath = lang_path ($ defaultLocale );
34- $ sitePath = "{$ langPath }/site .php " ;
35-
36- if (!File::exists ($ sitePath )) {
37+ $ seoPath = "{$ langPath }/seo .php " ;
38+
39+ if (!File::exists ($ seoPath )) {
3740 Log::warning (
38- "[메타 태그] {$ defaultLocale }/site .php 파일이 존재하지 않습니다. " .
41+ "[메타 태그] {$ defaultLocale }/seo .php 파일이 존재하지 않습니다. " .
3942 "SEO를 위해 다음과 같은 형식으로 파일을 생성해주세요: \n" .
4043 "return [ \n 'title' => '', \n 'description' => '', \n 'keywords' => '', \n]; \n"
4144 );
4245 } else {
4346 Log::warning (
44- "[메타 태그] {$ key } 번역 키가 {$ defaultLocale }/site .php 파일에 존재하지 않습니다. " .
47+ "[메타 태그] {$ key } 번역 키가 {$ defaultLocale }/seo .php 파일에 존재하지 않습니다. " .
4548 "SEO를 위해 해당 키를 추가해주세요. "
4649 );
4750 }
@@ -56,14 +59,20 @@ protected function registerMeta(): void
5659 $ this ->app ['config ' ]
5760 );
5861
62+ // 설정 값 가져오기
63+ $ metaConfig = Config::get ('essentials-entry.meta-tags ' , []);
64+ $ ogConfig = $ metaConfig ['og ' ] ?? [];
65+ $ imagesConfig = $ metaConfig ['images ' ] ?? [];
66+
5967 // 번역 키 확인
60- $ this ->checkTranslationKey ('site .title ' );
61- $ this ->checkTranslationKey ('site .description ' );
62- $ this ->checkTranslationKey ('site .keywords ' );
68+ $ this ->checkTranslationKey ('seo .title ' );
69+ $ this ->checkTranslationKey ('seo .description ' );
70+ $ this ->checkTranslationKey ('seo .keywords ' );
6371
64- $ meta ->setTitle (__ ('site.title ' ));
72+ // 기본 태그 설정
73+ $ meta ->setTitle (__ ('seo.title ' ));
6574 $ meta ->addTag ('variables ' , new JavascriptVariables ([
66- 'appTitle ' => __ ('site .title ' ),
75+ 'appTitle ' => __ ('seo .title ' ),
6776 ]));
6877
6978 // 파비콘 설정
@@ -72,18 +81,30 @@ protected function registerMeta(): void
7281 $ meta ->setFavicon ($ faviconConfig ['path_rewrite ' ]);
7382 }
7483
75- $ meta ->setDescription (__ ('site.description ' ));
76- $ meta ->setKeywords (__ ('site.keywords ' ));
77- $ meta ->setRobots ('index,follow ' );
78- $ meta ->setViewport ('width=device-width, initial-scale=1 ' );
79- $ meta ->setCharset ('utf-8 ' );
84+ // 애플 터치 아이콘 설정
85+ if (isset ($ imagesConfig ['touch_icon ' ]) && !empty ($ imagesConfig ['touch_icon ' ])) {
86+ $ meta ->addLink ('apple-touch-icon ' , [
87+ 'rel ' => 'apple-touch-icon ' ,
88+ 'href ' => $ imagesConfig ['touch_icon ' ],
89+ ]);
90+ }
91+
92+ // 기본 메타 태그 설정
93+ $ meta ->setDescription (__ ('seo.description ' ));
94+ $ meta ->setKeywords (__ ('seo.keywords ' ));
8095
81- $ og = new OpenGraphPackage ('' );
82- $ og ->setType ('website ' )
83- ->setSiteName (Config::get ('app.name ' ))
96+ // 오픈 그래프 패키지 설정
97+ $ og = new OpenGraphPackage ('og-package ' );
98+ $ og ->setType ($ ogConfig ['type ' ] ?? 'website ' )
99+ ->setSiteName (__ ('seo.title ' ))
84100 ->setLocale (App::getLocale ());
85101
86- $ supportedLocales = array_keys (Config::get ('essentials-entry.language.supported ' , []));
102+ if (isset ($ ogConfig ['image ' ]) && !empty ($ ogConfig ['image ' ])) {
103+ $ og ->addImage ((string ) url ($ ogConfig ['image ' ]));
104+ }
105+
106+ // 다국어 설정
107+ $ supportedLocales = EssentialsEntryFacade::getSupportedLanguages ();
87108 foreach ($ supportedLocales as $ locale ) {
88109 $ og ->addAlternateLocale ($ locale );
89110 }
@@ -98,38 +119,53 @@ protected function registerMeta(): void
98119 ]);
99120 }
100121
101- // Get current route information
122+ // 현재 라우트 정보 가져오기
102123 if (Route::isLocalized () || Route::isFallback ()) {
103- // Set canonical URL for current locale
124+ // 현재 로케일에 대한 표준 URL 설정
104125 try {
105126 $ currentLocale = App::getLocale ();
106127 $ canonicalUrl = Route::localizedUrl ($ currentLocale );
107128 $ meta ->setCanonical ($ canonicalUrl );
108129 $ og ->setUrl ($ canonicalUrl );
109130 } catch (\Exception $ e ) {
110- // Skip if URL generation fails
131+ // URL 생성에 실패하면 스킵
111132 }
112133
113- // Add x-default first
134+ // x-default를 먼저 추가
114135 try {
115- $ url = Route::localizedUrl ('en ' ); // 영어를 기본값으로 설정
136+ $ defaultLocale = Config::get ('essentials-entry.language.default ' , 'en ' );
137+ $ url = Route::localizedUrl ($ defaultLocale );
116138 $ meta ->setHrefLang ('x-default ' , $ url );
117139 } catch (\Exception $ e ) {
118- // Skip if URL generation fails
140+ // URL 생성에 실패하면 스킵
119141 }
120142
143+ // 각 지원 로케일에 대한 대체 URL 추가
121144 foreach ($ supportedLocales as $ locale ) {
122145 try {
123146 $ url = Route::localizedUrl ($ locale );
124147 $ meta ->setHrefLang ($ locale , $ url );
125148 } catch (\Exception $ e ) {
126- // Skip if URL generation fails for this locale
149+ // 이 로케일에 대한 URL 생성에 실패하면 스킵
127150 continue ;
128151 }
129152 }
130153 }
131154
155+ // 전역 JavaScript 변수
156+ $ jsVariables = [
157+ 'appTitle ' => __ ('seo.title ' ),
158+ 'locale ' => App::getLocale (),
159+ 'baseUrl ' => url ('/ ' )
160+ ];
161+
162+ // JavaScript 변수 추가
163+ $ meta ->addTag ('variables ' , new JavascriptVariables ($ jsVariables ));
164+
165+ // OG 패키지 등록
132166 $ meta ->registerPackage ($ og );
167+
168+ // 초기화 (기본값 가져오기 및 태그 생성, 기본 패키지 포함)
133169 $ meta ->initialize ();
134170
135171 return $ meta ;
0 commit comments