Skip to content
Permalink
Browse files

domain 기반 locale 처리 및 세션 공유

  • Loading branch information...
jhyeon1010 committed Jun 27, 2019
1 parent 9e2e892 commit d40325d4f55880159c58dd0e14e425e16ef9fd5e
@@ -67,6 +67,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\ShareLocalizeSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
// \App\Http\Middleware\VerifyCsrfToken::class,
@@ -0,0 +1,98 @@
<?php
/**
* ShareLocalizeSession.php
*
* PHP version 7
*
* @category Middleware
* @package App\Http\Middleware
* @author XE Developers <developers@xpressengine.com>
* @copyright 2015 Copyright (C) NAVER Corp. <http://www.navercorp.com>
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html LGPL-2.1
* @link https://xpressengine.io
*/
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Foundation\Application;
/**
* Class ShareLocalizeSession
*
* @category Middleware
* @package App\Http\Middleware
* @author XE Developers <developers@xpressengine.com>
* @copyright 2015 Copyright (C) NAVER Corp. <http://www.navercorp.com>
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html LGPL-2.1
* @link https://xpressengine.io
*/
class ShareLocalizeSession
{
/**
* Application instance
*
* @var Application
*/
private $app;
/**
* LangPreprocessor constructor.
*
* @param Application $app Application instance
*/
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request request
* @param \Closure $next to be called next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($id = $this->getShareSessionId($request)) {
$this->app['session']->flush();
$this->app['session']->setId($id);
$this->app['session']->start();
$queries = array_except($request->query->all(), '_s');
return redirect($request->url().(count($queries) > 0 ? '?'.http_build_query($queries) : ''));
}
return $next($request);
}
/**
* Get the session id for shared
*
* @param \Illuminate\Http\Request $request request
* @return null|string
*/
protected function getShareSessionId($request)
{
if (!$this->isShouldSessionBeShared($request)) {
return null;
}
return $this->app['session']->isValidId($id = decrypt($request->get('_s'))) ? $id : null;
}
/**
* Determine if the session id should be shared
*
* @param \Illuminate\Http\Request $request request
* @return bool
*/
protected function isShouldSessionBeShared($request)
{
return $this->app['config']['xe.lang.locale_type'] === 'domain' &&
$request->method() === 'GET' &&
$request->get('_s') &&
!$this->app['auth']->check();
}
}
@@ -28,16 +28,6 @@
*/
class HttpServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
@@ -51,7 +41,7 @@ public function register()
$config = $app['config'];
$request->setConfig($config);
if ($config['xe.lang.locale_route'] === true) {
if ($config['xe.lang.locale_type'] === 'route') {
if (in_array($locale = $request->rawSegment(1), $config['xe.lang.locales'])) {
$app['url']->formatHostUsing(function ($root) use ($locale) {
return rtrim($root, '/') . '/' . $locale;
@@ -60,6 +50,11 @@ public function register()
} else {
$locale = $this->getFallbackLocale();
}
} elseif ($config['xe.lang.locale_type'] === 'domain') {
$locale = array_search($request->getHttpHost(), $config['xe.lang.locale_domains']);
if ($locale === false) {
$locale = $this->getFallbackLocale();
}
} else {
$locale = $request->get('_l') ?: $request->cookie('locale');
if (!in_array($locale, $config['xe.lang.locales'])) {
@@ -77,7 +72,6 @@ public function register()
protected function getFallbackLocale()
{
// return $this->app['config']['app.fallback_locale'];
return $this->app['xe.translator']->getLocale();
}
}
@@ -561,11 +561,11 @@ private function registerSettingsPermissions()
*/
private function setProfileImageResolverOfUser()
{
$default = $this->app['config']['xe.user.profileImage.default'];
$storage = $this->app['xe.storage'];
$media = $this->app['xe.media'];
User::setProfileImageResolver(
function ($imageId) use ($default, $storage, $media) {
function ($imageId) {
$default = $this->app['config']['xe.user.profileImage.default'];
$storage = $this->app['xe.storage'];
$media = $this->app['xe.media'];
try {
if($imageId !== null) {
/** @var Storage $storage */
@@ -29,7 +29,12 @@
'en' => 'English',
],
'autocomplete' => false,
'locale_route' => true,
'locale_type' => 'null', // null(cookie), route, domain
'locale_domains' => [
'ko' => 'example1.test',
'en' => 'example2.test',
]
],
'settings' => [
@@ -285,14 +285,23 @@ function locale_url($locale)
{
$request = app('request');
$queries = $request->query->all();
if (config('xe.lang.locale_route') === true) {
if (config('xe.lang.locale_type') === 'route') {
$url = $request->root().'/'.$locale.'/'.$request->path();
} elseif (config('xe.lang.locale_type') === 'domain') {
$domains = config('xe.lang.locale_domains');
if (!isset($domains[$locale])) {
throw new \Exception("Unknown locale [$locale]");
}
$url = $request->getScheme().'://'.$domains[$locale].'/'.$request->path();
if (auth()->check()) {
array_set($queries, '_s', encrypt(session()->getId()));
}
} else {
$url = $request->url();
array_set($queries, '_l', $locale);
}
return $url.'?'.http_build_query($queries);
return $url.(count($queries) > 0 ? '?'.http_build_query($queries) : '');
}
}

0 comments on commit d40325d

Please sign in to comment.
You can’t perform that action at this time.