Skip to content

Instantly share code, notes, and snippets.

@kca66y
Created January 7, 2017 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kca66y/12b0d6c056555d41a986b8f080213dca to your computer and use it in GitHub Desktop.
Save kca66y/12b0d6c056555d41a986b8f080213dca to your computer and use it in GitHub Desktop.
<?php namespace App\Http\Middleware;
use Closure;
use App\Http\Models\City;
class SiteUrl
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
function rus2translit($string) {
$converter = array(
'а' => 'a', 'б' => 'b', 'в' => 'v',
'г' => 'g', 'д' => 'd', 'е' => 'e',
'ё' => 'e', 'ж' => 'zh', 'з' => 'z',
'и' => 'i', 'й' => 'y', 'к' => 'k',
'л' => 'l', 'м' => 'm', 'н' => 'n',
'о' => 'o', 'п' => 'p', 'р' => 'r',
'с' => 's', 'т' => 't', 'у' => 'u',
'ф' => 'f', 'х' => 'h', 'ц' => 'c',
'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch',
'ь' => '', 'ы' => 'y', 'ъ' => '',
'э' => 'e', 'ю' => 'yu', 'я' => 'ya',
' ' => '_', '-' => '-', '.' => '', '!' => '', '?' => '', '&' => '', "'" => '', "%" => "proc",
'А' => 'A', 'Б' => 'B', 'В' => 'V',
'Г' => 'G', 'Д' => 'D', 'Е' => 'E',
'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z',
'И' => 'I', 'Й' => 'Y', 'К' => 'K',
'Л' => 'L', 'М' => 'M', 'Н' => 'N',
'О' => 'O', 'П' => 'P', 'Р' => 'R',
'С' => 'S', 'Т' => 'T', 'У' => 'U',
'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sch',
'Ь' => '', 'Ы' => 'Y', 'Ъ' => '',
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya',
);
return strtr($string, $converter);
}
public function handle($request, Closure $next)
{
$response = $next($request);
$locale = \Request::cookie('current_city');
if ($locale != ''){
$langs = City::find($locale)->title;
$response = SiteUrl::rus2translit($langs);
return redirect( $response .'/'. $request->path());
}
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment