From 3bb8222120077888302498a2602e1bdabe0cf75e Mon Sep 17 00:00:00 2001 From: A1Gard Date: Tue, 13 Aug 2024 20:17:01 +0330 Subject: [PATCH] added multi-lang support client fixed rtl bootstrap file for rtl lang WIP: need fixed for messages maybe sessions --- app/Helpers/Helper.php | 29 +++- app/Http/Controllers/ClientController.php | 79 +++++++++-- app/Http/Middleware/LangControl.php | 45 ++++++ app/Models/Attachment.php | 2 +- app/Models/Category.php | 2 +- app/Models/Clip.php | 2 +- app/Models/Gallery.php | 2 +- app/Models/Group.php | 2 +- app/Models/Post.php | 2 +- app/Models/Product.php | 2 +- .../views/website/inc/website-head.blade.php | 4 + routes/web.php | 131 +++++++++--------- 12 files changed, 216 insertions(+), 86 deletions(-) create mode 100644 app/Http/Middleware/LangControl.php diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index edf4f3d..46f7b6f 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -983,7 +983,7 @@ function isGuestMaxAttemptTry($action, $max = 5, $minutes = 60) */ function homeUrl() { - return \route('client.welcome'); + return fixUrlLang(\route('client.welcome')); } /** @@ -992,7 +992,7 @@ function homeUrl() */ function postsUrl() { - return \route('client.posts'); + return fixUrlLang(\route('client.posts')); } /** * products url to best experience for multi lang shops @@ -1000,7 +1000,7 @@ function postsUrl() */ function productsUrl() { - return \route('client.products'); + return fixUrlLang(\route('client.products')); } /** * clips url to best experience for multi lang shops @@ -1008,7 +1008,7 @@ function productsUrl() */ function clipsUrl() { - return \route('client.clips'); + return fixUrlLang(\route('client.clips')); } /** * galleries url to best experience for multi lang shops @@ -1016,7 +1016,7 @@ function clipsUrl() */ function gallariesUrl() { - return \route('client.galleries'); + return fixUrlLang(\route('client.galleries')); } /** * attachments url to best experience for multi lang shops @@ -1024,7 +1024,7 @@ function gallariesUrl() */ function attachmentsUrl() { - return \route('client.attachments'); + return fixUrlLang(\route('client.attachments')); } /** @@ -1033,7 +1033,7 @@ function attachmentsUrl() */ function tagUrl($slug) { - return route('client.tag', $slug); + return fixUrlLang(route('client.tag', $slug)); } function usableProp($props) @@ -1157,3 +1157,18 @@ RESULT; } + + +/** + * fix url for multilang shops + * @param $url + * @return array|mixed|string|string[] + */ +function fixUrlLang($url) +{ + if (app()->getLocale() != config('app.xlang.main')){ + $welcome = \route('client.welcome'); + return str_replace($welcome,$welcome .'/'.app()->getLocale(),$url); + } + return $url; +} diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 73539a9..f96d968 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -15,6 +15,7 @@ use App\Models\Quantity; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Route; use Plank\Metable\Meta; use Spatie\Tags\Tag; @@ -32,9 +33,11 @@ class ClientController extends Controller return view('client.welcome', compact('area', 'title', 'subtitle')); } - public function post(Post $post) + public function post($slug) { + $post = Post::where('slug', $slug)->firstOrFail(); + if ($post->status = 0 && !auth()->check()) { return abort(403); } @@ -50,9 +53,11 @@ class ClientController extends Controller return view('client.post', compact('area', 'post', 'title', 'subtitle', 'breadcrumb')); } - public function clip(Clip $clip) + public function clip($slug) { + $clip = Clip::where('slug', $slug)->firstOrFail(); + if ($clip->status = 0 && !auth()->check()) { return abort(403); } @@ -66,8 +71,10 @@ class ClientController extends Controller return view('client.default-list', compact('area', 'clip', 'title', 'subtitle', 'breadcrumb')); } - public function gallery(Gallery $gallery) + public function gallery($slug) { + + $gallery = Gallery::where('slug', $slug)->firstOrFail(); if ($gallery->status = 0 && !auth()->check()) { return abort(403); } @@ -133,8 +140,10 @@ class ClientController extends Controller return view('client.default-list', compact('area', 'attachs', 'title', 'subtitle')); } - public function attachment(Attachment $attachment) + public function attachment($slug) { + + $attachment = Attachment::where('slug', $slug)->firstOrFail(); $area = 'attachment'; $title = $attachment->title; $subtitle = $attachment->subtitle; @@ -221,8 +230,10 @@ class ClientController extends Controller return view('client.group', compact('area', 'posts', 'title', 'subtitle', 'group', 'breadcrumb')); } - public function product(Product $product) + public function product($slug) { + + $product = Product::where('slug', $slug)->firstOrFail(); if ($product->status = 0 && !auth()->check()) { return abort(403); } @@ -240,8 +251,9 @@ class ClientController extends Controller return view('client.default-list', compact('area', 'product', 'title', 'subtitle', 'breadcrumb')); } - public function category(Category $category, Request $request) + public function category($slug, Request $request) { + $category = Category::where('slug', $slug)->firstOrFail(); $area = 'category'; $title = $category->name; $subtitle = $category->subtitle; @@ -356,8 +368,9 @@ class ClientController extends Controller return view('client.category', compact('area', 'products', 'title', 'subtitle', 'category', 'breadcrumb')); } - public function attachDl(Attachment $attachment) + public function attachDl($slug) { + $attachment = Attachment::where('slug', $slug)->firstOrFail(); $attachment->increment('downloads'); $file = (storage_path() . '/app/public/attachments/' . $attachment->file); if (file_exists($file)) { @@ -366,8 +379,10 @@ class ClientController extends Controller } - public function productCompareToggle(Product $product) + public function productCompareToggle($slug) { + + $product = Product::where('slug', $slug)->firstOrFail(); if (\Cookie::has('compares')) { $compares = json_decode(\Cookie::get('compares'), true); if (in_array($product->id, $compares)) { @@ -390,8 +405,10 @@ class ClientController extends Controller } } - public function ProductFavToggle(Product $product) + public function ProductFavToggle($slug) { + $product = Product::where('slug', $slug)->firstOrFail(); + if (!auth('customer')->check()) { return errors([ __("You need to login first"), @@ -544,8 +561,50 @@ class ClientController extends Controller } - public function sitemap(){ + public function sitemap() + { header('content-type: text/xml'); return view('website.sitemap'); } + + public function lang(Request $request) + { + + $uri = '/' . $request->path(); + // Iterate through all the defined routes + $r = null; + $n = ''; + foreach (Route::getRoutes() as $route) { + + // Just check client routes + if (substr($route->getName(), 0, 7) != 'client.' || $route->getName() == 'client.welcome') { + continue; + } + + $uri2 = str_replace('/', '\\/', $route->uri()); + $uri2 = preg_replace('/\{[a-z]*\}/m', '.*', $uri2); + // Check if the route matches the given URI + if (preg_match('/' . $uri2 . '/', $uri)) { + $r = $route->action['controller']; + $n = $route->uri(); + break; + } + } + $method = explode('@', $r)[1]; + $segments = $request->segments(); + $routes = explode('/',$n); + $args = []; + foreach ($routes as $i => $route) { + if ($route[0] == '{'){ + $args[] = $segments[$i]; + } + } +// dd($segments); + return $this->$method(...$args); + } + + public function langIndex() + { + return $this->welcome(); + } } diff --git a/app/Http/Middleware/LangControl.php b/app/Http/Middleware/LangControl.php new file mode 100644 index 0000000..71b54c4 --- /dev/null +++ b/app/Http/Middleware/LangControl.php @@ -0,0 +1,45 @@ +segments(); + if (strlen($segments[0]) == 2 && preg_match('/[A-Za-z]/', $segments[0])) { + app()->setLocale($segments[0]); + } else { + app()->setLocale(config('app.locale')); + } + array_shift($segments); + $url = \request()->path(); + $url = str_replace(app()->getLocale(), '', $url); + // Modify the request + $newPath = '/' . implode('/', $segments); + $newUrl = $request->root() . $newPath . ($request->getQueryString() ? '?'.$request->getQueryString() : ''); + + $request->server->set('REQUEST_URI', $newPath); + $request->initialize( + $request->query->all(), + $request->request->all(), + $request->attributes->all(), + $request->cookies->all(), + $request->files->all(), + $request->server->all() + ); + + return $next($request); + } +} diff --git a/app/Models/Attachment.php b/app/Models/Attachment.php index f1ffe48..fb53cc5 100644 --- a/app/Models/Attachment.php +++ b/app/Models/Attachment.php @@ -65,7 +65,7 @@ class Attachment extends Model public function webUrl() { - return route('client.attachment',$this->slug); + return fixUrlLang(route('client.attachment',$this->slug)); } } diff --git a/app/Models/Category.php b/app/Models/Category.php index 8e6c447..df8abf0 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -77,7 +77,7 @@ class Category extends Model public function webUrl() { - return route('client.category',$this->slug); + return fixUrlLang(route('client.category',$this->slug)); } public function products() diff --git a/app/Models/Clip.php b/app/Models/Clip.php index a2ce545..d6c046d 100644 --- a/app/Models/Clip.php +++ b/app/Models/Clip.php @@ -55,7 +55,7 @@ class Clip extends Model } public function webUrl(){ - return route('client.clip',$this->slug); + return fixUrlLang(route('client.clip',$this->slug)); } diff --git a/app/Models/Gallery.php b/app/Models/Gallery.php index 4159ff1..cb5fdea 100644 --- a/app/Models/Gallery.php +++ b/app/Models/Gallery.php @@ -71,7 +71,7 @@ class Gallery extends Model implements HasMedia } public function webUrl(){ - return route('client.gallery',$this->slug); + return fixUrlLang(route('client.gallery',$this->slug)); } diff --git a/app/Models/Group.php b/app/Models/Group.php index 12416b8..ceacf03 100644 --- a/app/Models/Group.php +++ b/app/Models/Group.php @@ -84,7 +84,7 @@ class Group extends Model public function webUrl() { - return route('client.group',$this->slug); + return fixUrlLang(route('client.group',$this->slug)); } public function published($limit = 10, $order = 'id', $dir = 'DESC') diff --git a/app/Models/Post.php b/app/Models/Post.php index 32659b9..655809d 100644 --- a/app/Models/Post.php +++ b/app/Models/Post.php @@ -131,7 +131,7 @@ class Post extends Model implements HasMedia // } public function webUrl(){ - return route('client.post',$this->slug); + return fixUrlLang(route('client.post',$this->slug)); } diff --git a/app/Models/Product.php b/app/Models/Product.php index 5700818..3cedd24 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -283,7 +283,7 @@ class Product extends Model implements HasMedia public function webUrl() { - return route('client.product',$this->slug); + return fixUrlLang(route('client.product',$this->slug)); } diff --git a/resources/views/website/inc/website-head.blade.php b/resources/views/website/inc/website-head.blade.php index 7607d86..007e567 100644 --- a/resources/views/website/inc/website-head.blade.php +++ b/resources/views/website/inc/website-head.blade.php @@ -11,7 +11,11 @@ @yield('title') + @if(langIsRTL(config('app.locale'))) + + @else + @endif @vite(['resources/sass/client.scss', 'resources/js/client.js']) diff --git a/routes/web.php b/routes/web.php index 8291a55..9cef5fb 100644 --- a/routes/web.php +++ b/routes/web.php @@ -2,8 +2,7 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Route; - - +use App\Http\Controllers\ClientController; Auth::routes(['register' => false]); @@ -17,13 +16,13 @@ Route::prefix(config('app.panel.prefix'))->name('admin.')->group( function () { - Route::get('/',[\App\Http\Controllers\HomeController::class,'index'])->name('dash'); + Route::get('/', [\App\Http\Controllers\HomeController::class, 'index'])->name('dash'); - Route::post('ckeditor/upload', [\App\Http\Controllers\Admin\CkeditorController::class,'upload'])->name('ckeditor.upload'); - Route::get('adminlogs', [\App\Http\Controllers\Admin\AdminLogController::class,'index'])->name('adminlog.index'); - Route::get('adminlogs/{user}', [\App\Http\Controllers\Admin\AdminLogController::class,'log'])->name('adminlog.show'); - Route::post('images/store/{gallery}', [\App\Http\Controllers\Admin\ImageController::class,'store'])->name('image.store'); - Route::get('images/destroy/{image}', [\App\Http\Controllers\Admin\ImageController::class,'destroy'])->name('image.destroy'); + Route::post('ckeditor/upload', [\App\Http\Controllers\Admin\CkeditorController::class, 'upload'])->name('ckeditor.upload'); + Route::get('adminlogs', [\App\Http\Controllers\Admin\AdminLogController::class, 'index'])->name('adminlog.index'); + Route::get('adminlogs/{user}', [\App\Http\Controllers\Admin\AdminLogController::class, 'log'])->name('adminlog.show'); + Route::post('images/store/{gallery}', [\App\Http\Controllers\Admin\ImageController::class, 'store'])->name('image.store'); + Route::get('images/destroy/{image}', [\App\Http\Controllers\Admin\ImageController::class, 'destroy'])->name('image.destroy'); Route::prefix('users')->name('user.')->group( @@ -354,76 +353,84 @@ Route::prefix(config('app.panel.prefix'))->name('admin.')->group( }); -Route::get('theme/variable.css',[\App\Http\Controllers\ThemeController::class,'cssVariables'])->name('theme.variable.css'); +Route::get('theme/variable.css', [\App\Http\Controllers\ThemeController::class, 'cssVariables'])->name('theme.variable.css'); Route::middleware([\App\Http\Middleware\VisitorCounter::class]) - ->name('client.')->group(function (){ - // index - Route::get('/', [\App\Http\Controllers\ClientController::class,'welcome'])->name('welcome'); - Route::get('/posts', [\App\Http\Controllers\ClientController::class,'posts'])->name('posts'); - Route::get('/customer/sign-out', [\App\Http\Controllers\ClientController::class,'signOut'])->name('sign-out'); - Route::post('/customer/sign-in/do', [\App\Http\Controllers\ClientController::class,'singInDo'])->name('sign-in-do'); - Route::get('/customer/sign-in', [\App\Http\Controllers\ClientController::class,'signIn'])->name('sign-in'); - Route::get('/customer/sign-up', [\App\Http\Controllers\ClientController::class,'signUp'])->name('sign-up'); - Route::get('/customer/send/auth-code', [\App\Http\Controllers\ClientController::class,'sendSms'])->name('send-sms'); - Route::get('/customer/check/auth-code', [\App\Http\Controllers\ClientController::class,'checkAuth'])->name('check-auth'); - Route::get('/customer/profile', [\App\Http\Controllers\ClientController::class,'profile'])->name('profile'); - Route::get('/compare', [\App\Http\Controllers\ClientController::class,'compare'])->name('compare'); - Route::get('/galleries', [\App\Http\Controllers\ClientController::class,'galleries'])->name('galleries'); - Route::get('/videos', [\App\Http\Controllers\ClientController::class,'clips'])->name('clips'); - Route::post('/card/check', [\App\Http\Controllers\CardController::class,'check'])->name('card.check'); - Route::get('/card/discount/{code}', [\App\Http\Controllers\CardController::class,'discount'])->name('card.discount'); - Route::get('/card', [\App\Http\Controllers\CardController::class,'index'])->name('card'); - Route::get('/cardClear',[\App\Http\Controllers\CardController::class,'clearing'])->name('card.clear'); - Route::get('/profile', [\App\Http\Controllers\CustomerController::class,'profile'])->name('profile'); - Route::post('/profile/save', [\App\Http\Controllers\CustomerController::class,'save'])->name('profile.save'); - Route::get('/invoice/{invoice}', [\App\Http\Controllers\CustomerController::class,'invoice'])->name('invoice'); - Route::get('/products', [\App\Http\Controllers\ClientController::class,'products'])->name('products'); - Route::get('/attachments', [\App\Http\Controllers\ClientController::class,'attachments'])->name('attachments'); - Route::get('/attachment/{attachment}', [\App\Http\Controllers\ClientController::class,'attachment'])->name('attachment'); - Route::get('/tag/{slug}', [\App\Http\Controllers\ClientController::class,'tag'])->name('tag'); // wip - Route::get('/group/{slug}', [\App\Http\Controllers\ClientController::class,'group'])->name('group'); // wip - Route::get('/product/{product}', [\App\Http\Controllers\ClientController::class,'product'])->name('product'); - Route::get('/video/{clip}', [\App\Http\Controllers\ClientController::class,'clip'])->name('clip'); - Route::get('/category/{category}', [\App\Http\Controllers\ClientController::class,'category'])->name('category'); - Route::get('/gallery/{gallery}', [\App\Http\Controllers\ClientController::class,'gallery'])->name('gallery'); - Route::get('/search', [\App\Http\Controllers\ClientController::class,'search'])->name('search'); - Route::get('attach/download/{attachment}', [\App\Http\Controllers\ClientController::class,'attachDl'])->name('attach-dl'); - Route::get('/post/{post}', [\App\Http\Controllers\ClientController::class,'post'])->name('post'); - - Route::get('product/fav/toggle/{product}', [\App\Http\Controllers\ClientController::class, 'ProductFavToggle'])->name('product-fav-toggle'); - Route::get('product/compare/toggle/{product}', [\App\Http\Controllers\ClientController::class, 'productCompareToggle'])->name('product-compare-toggle'); - Route::get('card/toggle/{product}', [\App\Http\Controllers\CardController::class, 'productCardToggle'])->name('product-card-toggle'); - - Route::post('/comment/submit', [\App\Http\Controllers\ClientController::class,'submitComment'])->name('comment.submit'); -}); - - -Route::get('/sitemap.xml',[\App\Http\Controllers\ClientController::class,'sitemap'])->name('sitemap'); + ->name('client.')->group(function () { + // index + Route::get('/', [ClientController::class, 'welcome'])->name('welcome'); + Route::get('/posts', [ClientController::class, 'posts'])->name('posts'); + Route::get('/customer/sign-out', [ClientController::class, 'signOut'])->name('sign-out'); + Route::post('/customer/sign-in/do', [ClientController::class, 'singInDo'])->name('sign-in-do'); + Route::get('/customer/sign-in', [ClientController::class, 'signIn'])->name('sign-in'); + Route::get('/customer/sign-up', [ClientController::class, 'signUp'])->name('sign-up'); + Route::get('/customer/send/auth-code', [ClientController::class, 'sendSms'])->name('send-sms'); + Route::get('/customer/check/auth-code', [ClientController::class, 'checkAuth'])->name('check-auth'); + Route::get('/customer/profile', [ClientController::class, 'profile'])->name('profile'); + Route::get('/compare', [ClientController::class, 'compare'])->name('compare'); + Route::get('/galleries', [ClientController::class, 'galleries'])->name('galleries'); + Route::get('/videos', [ClientController::class, 'clips'])->name('clips'); + Route::post('/card/check', [\App\Http\Controllers\CardController::class, 'check'])->name('card.check'); + Route::get('/card/discount/{code}', [\App\Http\Controllers\CardController::class, 'discount'])->name('card.discount'); + Route::get('/card', [\App\Http\Controllers\CardController::class, 'index'])->name('card'); + Route::get('/cardClear', [\App\Http\Controllers\CardController::class, 'clearing'])->name('card.clear'); + Route::get('/profile', [\App\Http\Controllers\CustomerController::class, 'profile'])->name('profile'); + Route::post('/profile/save', [\App\Http\Controllers\CustomerController::class, 'save'])->name('profile.save'); + Route::get('/invoice/{invoice}', [\App\Http\Controllers\CustomerController::class, 'invoice'])->name('invoice'); + Route::get('/products', [ClientController::class, 'products'])->name('products'); + Route::get('/attachments', [ClientController::class, 'attachments'])->name('attachments'); + Route::get('/attachment/{attachment}', [ClientController::class, 'attachment'])->name('attachment'); + Route::get('/tag/{slug}', [ClientController::class, 'tag'])->name('tag'); // wip + Route::get('/group/{slug}', [ClientController::class, 'group'])->name('group'); // wip + Route::get('/product/{product}', [ClientController::class, 'product'])->name('product'); + Route::get('/video/{clip}', [ClientController::class, 'clip'])->name('clip'); + Route::get('/category/{category}', [ClientController::class, 'category'])->name('category'); + Route::get('/gallery/{gallery}', [ClientController::class, 'gallery'])->name('gallery'); + Route::get('/search', [ClientController::class, 'search'])->name('search'); + Route::get('attach/download/{attachment}', [ClientController::class, 'attachDl'])->name('attach-dl'); + Route::get('/post/{post}', [ClientController::class, 'post'])->name('post'); + + Route::get('product/fav/toggle/{product}', [ClientController::class, 'ProductFavToggle'])->name('product-fav-toggle'); + Route::get('product/compare/toggle/{product}', [ClientController::class, 'productCompareToggle'])->name('product-compare-toggle'); + Route::get('card/toggle/{product}', [\App\Http\Controllers\CardController::class, 'productCardToggle'])->name('product-card-toggle'); + + Route::post('/comment/submit', [ClientController::class, 'submitComment'])->name('comment.submit'); + }); + + +Route::get('/sitemap.xml', [ClientController::class, 'sitemap'])->name('sitemap'); // to developer test -Route::get('login/as/{mobile}',function ($mobile){ - if (auth()->check() && auth()->user()->hasRole('developer') ){ +Route::get('login/as/{mobile}', function ($mobile) { + if (auth()->check() && auth()->user()->hasRole('developer')) { return \Auth::guard('customer') - ->loginUsingId(\App\Models\Customer::where('mobile',$mobile)->first()->id); - } else{ + ->loginUsingId(\App\Models\Customer::where('mobile', $mobile)->first()->id); + } else { return abort(403); } })->name('login.as'); -Route::get('test',function (){ +Route::get('test', function () { // return \Resources\Views\Segments\PreloaderCircle::onAdd(); // return $product->getAllMeta(); // return \App\Models\Quantity::whereId(1)->first()->meta; - return [json_decode(\Cookie::get('card')),json_decode(\Cookie::get('q'))]; + return [json_decode(\Cookie::get('card')), json_decode(\Cookie::get('q'))]; })->name('test'); - - -Route::get('whoami',function (){ - if (!auth('customer')->check()){ +Route::get('whoami', function () { + if (!auth('customer')->check()) { return 'You are nothing'; } return \Auth::guard('customer')->user(); })->name('whoami'); + +Route::any('{lang}/{any}', [ClientController::class, 'lang']) + ->where('any', '.*') + ->where('lang','[A-Za-z]{2}') + ->middleware([\App\Http\Middleware\LangControl::class]); +Route::any('{lang}', [ClientController::class, 'langIndex']) + ->where('lang','[A-Za-z]{2}') + ->middleware([\App\Http\Middleware\LangControl::class]); + +