diff --git a/src/app/Http/Middleware/DeviceDetect.php b/src/app/Http/Middleware/DeviceDetect.php index d1b40846..9b49e50c 100644 --- a/src/app/Http/Middleware/DeviceDetect.php +++ b/src/app/Http/Middleware/DeviceDetect.php @@ -8,9 +8,15 @@ use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cookie; +use Jenssegers\Agent\Facades\Agent; class DeviceDetect { + /** + * Web ID for robot devices + */ + private const WEB_ID_FOR_ROBOT_DEVICES = '6b8945bed1e18bc4f2d1691ac63f5f56'; + /** * Handle an incoming request. * @@ -18,7 +24,7 @@ class DeviceDetect */ public function handle(Request $request, Closure $next) { - $webId = $request->cookie(CookieEnum::DEVICE_ID->value); + $webId = $this->getDeviceWebId($request); if (!$webId) { $webId = UserDevice::generateNewWebId($request); Cookie::queue( @@ -32,4 +38,24 @@ public function handle(Request $request, Closure $next) return $next($request); } + + /** + * Get the web ID for the current device + */ + private function getDeviceWebId(Request $request): ?string + { + if ($this->isRobot($request)) { + return self::WEB_ID_FOR_ROBOT_DEVICES; + } + + return $request->cookie(CookieEnum::DEVICE_ID->value); + } + + /** + * Check if the current request is from a robot + */ + private function isRobot(Request $request): bool + { + return Agent::isRobot(); // additional checks + } } diff --git a/src/app/Models/Url.php b/src/app/Models/Url.php index 08bbffd8..aeda5c4c 100644 --- a/src/app/Models/Url.php +++ b/src/app/Models/Url.php @@ -47,7 +47,7 @@ public static function search(string $slug): ?self */ public function model(): MorphTo { - return $this->morphTo(); + return $this->morphTo()->withTrashed(); } /** diff --git a/src/bootstrap/app.php b/src/bootstrap/app.php index 43ea03f5..9161fdb7 100644 --- a/src/bootstrap/app.php +++ b/src/bootstrap/app.php @@ -15,9 +15,15 @@ ->encryptCookies(['utm', Cookie::YANDEX_ID->value, Cookie::GOOGLE_ID->value]) ->throttleApi(redis: true); - $middleware->web(append: [ + $middleware->group('web', [ + \Illuminate\Cookie\Middleware\EncryptCookies::class, + \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, + \Illuminate\Session\Middleware\StartSession::class, + \Illuminate\View\Middleware\ShareErrorsFromSession::class, + \Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class, \App\Http\Middleware\DeviceDetect::class, \App\Http\Middleware\MigrateCartToDevice::class, + \Illuminate\Routing\Middleware\SubstituteBindings::class, \Spatie\GoogleTagManager\GoogleTagManagerMiddleware::class, \App\Http\Middleware\ViewMiddleware::class, ]); diff --git a/src/routes/web.php b/src/routes/web.php index 83bac202..232c7f43 100644 --- a/src/routes/web.php +++ b/src/routes/web.php @@ -72,7 +72,7 @@ $url = Url::search($slug); return $url?->model_type === Product::class - ? redirect(status: 301)->route('product.show', $url->model->slug) + ? redirect(status: 301)->route('product.show', $url->model()->firstOrFail()->slug) : app(CatalogController::class)->show($request); }; Route::get('catalog/city-{city}/{path?}', $check_catalog)->where('path', '[a-zA-Z0-9/_-]+')->name('shop-city');