Add (routes): All routes possible
Add (pages): home country page
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
if (! function_exists('is_empty')) {
|
||||
/**
|
||||
* A better function to check if a value is empty or null. Strings, arrays, and Objects are supported.
|
||||
@@ -35,3 +37,49 @@ function is_empty($value): bool
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('get_country_name_by_iso')) {
|
||||
function get_country_name_by_iso($country_iso)
|
||||
{
|
||||
if (! is_empty($country_iso)) {
|
||||
|
||||
$country_iso = strtoupper($country_iso);
|
||||
|
||||
try {
|
||||
return config("platform.country_codes.{$country_iso}")['name'];
|
||||
}
|
||||
catch (\Exception $e) {}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return 'International';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (! function_exists('get_country_emoji_by_iso')) {
|
||||
function get_country_emoji_by_iso($country_iso)
|
||||
{
|
||||
if (! is_empty($country_iso)) {
|
||||
|
||||
$country_iso = strtoupper($country_iso);
|
||||
|
||||
try {
|
||||
return config("platform.country_codes.{$country_iso}")['emoji'];
|
||||
}
|
||||
catch (\Exception $e) {}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return '🌎';
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('str_random')) {
|
||||
function str_random($length = 10)
|
||||
{
|
||||
return Str::random($length);
|
||||
}
|
||||
}
|
||||
@@ -7,23 +7,45 @@
|
||||
use App\Models\CountryLocale;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use Stevebauman\Location\Facades\Location;
|
||||
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
return redirect()->route('home.country', ['country' => 'my']);
|
||||
$country = strtolower($request->session()->get('country'));
|
||||
|
||||
return redirect()->route('home.country', ['country' => $country]);
|
||||
}
|
||||
|
||||
public function country(Request $request, $country)
|
||||
{
|
||||
$country = CountryLocale::where('slug', $country)->first();
|
||||
$country_locale = CountryLocale::where('slug', $country)->first();
|
||||
|
||||
if (! is_null($country)) {
|
||||
$categories = Category::where('country_locale_id', $country->id)->get();
|
||||
$categories = Category::where('country_locale_id', $country_locale->id)->get();
|
||||
|
||||
return view('front.country', ['categories' => $categories, 'country' => $country]);
|
||||
$request->session()->put('view_country_locale', $country_locale);
|
||||
|
||||
return view('front.country', ['categories' => $categories, 'country_locale' => $country_locale]);
|
||||
}
|
||||
|
||||
return redirect()->route('home.country', ['country' => 'my']);
|
||||
return redirect()->route('home.country', ['country' => config('platform.general.fallback_country_slug')]);
|
||||
}
|
||||
|
||||
public function countryCategory(Request $request, $country, $category)
|
||||
{
|
||||
return "{$country} : {$category}";
|
||||
}
|
||||
|
||||
public function posts(Request $request, $country)
|
||||
{
|
||||
return "{$country} : all posts";
|
||||
}
|
||||
|
||||
public function post(Request $request, $country, $post)
|
||||
{
|
||||
return "{$country} : {$post}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ class Kernel extends HttpKernel
|
||||
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
||||
\App\Http\Middleware\VerifyCsrfToken::class,
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
\App\Http\Middleware\StoreGeoSession::class,
|
||||
],
|
||||
|
||||
'api' => [
|
||||
|
||||
64
app/Http/Middleware/StoreGeoSession.php
Normal file
64
app/Http/Middleware/StoreGeoSession.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
|
||||
use Stevebauman\Location\Facades\Location;
|
||||
|
||||
use Galahad\TimezoneMapper\Facades\TimezoneMapper;
|
||||
|
||||
class StoreGeoSession
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$needs_update = false;
|
||||
|
||||
$ip = get_current_ip();
|
||||
$encoded_ip = base64_encode($ip);
|
||||
|
||||
if (! $request->session()->has('geokey')) {
|
||||
if ($request->session()->get('geokey') != $encoded_ip) {
|
||||
$request->session()->put('geokey', $encoded_ip);
|
||||
$needs_update = true;
|
||||
}
|
||||
} else {
|
||||
$request->session()->put('geokey', $encoded_ip);
|
||||
$needs_update = true;
|
||||
}
|
||||
|
||||
if (!$request->session()->has('timezone')) {
|
||||
$needs_update = true;
|
||||
}
|
||||
|
||||
if ($needs_update)
|
||||
{
|
||||
|
||||
if ($payload = Location::get($ip))
|
||||
{
|
||||
$request->session()->put('geodata', base64_encode(json_encode($payload)));
|
||||
$isocode = $payload->isoCode;
|
||||
}
|
||||
else
|
||||
{
|
||||
$isocode = '*';
|
||||
}
|
||||
|
||||
$request->session()->put('country', $isocode);
|
||||
|
||||
if (isset($payload->latitude) && isset($payload->longitude))
|
||||
{
|
||||
$request->session()->put('timezone', TimezoneMapper::mapCoordinates($payload->latitude, $payload->longitude));
|
||||
}
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
||||
|
||||
/**
|
||||
* Class Category
|
||||
@@ -30,7 +31,7 @@
|
||||
*/
|
||||
class Category extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
use SoftDeletes, Cachable;
|
||||
|
||||
protected $table = 'categories';
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
||||
|
||||
/**
|
||||
* Class CountryLocale
|
||||
@@ -24,7 +25,7 @@
|
||||
*/
|
||||
class CountryLocale extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
use SoftDeletes, Cachable;
|
||||
|
||||
protected $table = 'country_locales';
|
||||
|
||||
|
||||
46
app/Providers/ViewServiceProvider.php
Normal file
46
app/Providers/ViewServiceProvider.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\View\Composers\CategoryComposer;
|
||||
use App\View\Composers\CountryLocaleComposer;
|
||||
|
||||
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class ViewServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
// Using class based composers...
|
||||
View::composer('layouts.front.navigation', CategoryComposer::class);
|
||||
View::composer('layouts.front.navigation', CountryLocaleComposer::class);
|
||||
|
||||
View::composer('layouts.front.footer', CategoryComposer::class);
|
||||
View::composer('layouts.front.footer', CountryLocaleComposer::class);
|
||||
|
||||
if (auth()->check())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
15
app/View/Composers/CategoryComposer.php
Normal file
15
app/View/Composers/CategoryComposer.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Composers;
|
||||
|
||||
use App\Models\Category;
|
||||
|
||||
use Illuminate\View\View;
|
||||
|
||||
class CategoryComposer
|
||||
{
|
||||
public function compose(View $view)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
31
app/View/Composers/CountryLocaleComposer.php
Normal file
31
app/View/Composers/CountryLocaleComposer.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Composers;
|
||||
|
||||
use App\Models\CountryLocale;
|
||||
|
||||
use Illuminate\View\View;
|
||||
|
||||
class CountryLocaleComposer
|
||||
{
|
||||
public function compose(View $view)
|
||||
{
|
||||
$country_locales = CountryLocale::all();
|
||||
|
||||
$current_country_locale = null;
|
||||
|
||||
if (!is_null(request()->session()->get('view_country_locale')))
|
||||
{
|
||||
$current_country_locale = request()->session()->get('view_country_locale');
|
||||
}
|
||||
else
|
||||
{
|
||||
$current_country_locale = CountryLocale::where('slug', config('platform.general.fallback_country_slug'));
|
||||
}
|
||||
|
||||
|
||||
$view->with('country_locales', $country_locales)
|
||||
->with('current_country_locale', $current_country_locale);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user