mardi 12 septembre 2017

route or cache in Laravel project

I have a Laravel 5.4 project. There is a route issue that i can't solve second day. I have two routes like:

Route::get('/', 'HomeController@index')->name('index');

public function index(CookieJar $cookieJar, Request $request)
    {
        // Берем город из куков
        $city_from_cookie = Cookie::get('city');
        // Если города нет в куках
        if (!$city_from_cookie) {
            $client = new Client();
            $ip = $request->ip() == '127.0.0.1' ? '95.85.70.7' : $request->ip();
            $json = $client->request('GET', 'http://ift.tt/1esFEZI' . $ip . '');
            $city = json_decode($json->getBody()->getContents());

            if ($city->city === null) {
                $cookie = cookie('city', config('custom.city.default_slug'), 21600, null, null, false, true);
                $cookieJar->queue($cookie);
                $city = config('custom.city.default_slug');
            } else {
                $city = strtolower($city->city);

                try {
                    $city = City::findBySlugOrFail($city);
                    $city = $city->slug;
                } catch (ModelNotFoundException $ex) {
                    $city = 'moskva';
                }

                $cookie = cookie('city', $city, 21600, null, null, false, false);
                $cookieJar->queue($cookie);
            }

        } else {
            $city = $city_from_cookie;
        }

        return redirect(route('city', [$city]), 301);
    }

AND

Route::get('{city}', 'HomeController@getCityCategories')->name('city');

public function getCityCategories(City $city, CookieJar $cookieJar)
    {
//        Cache::flush();
//        dd(request()->getRequestUri());
        $cookie = cookie('city', $city->slug, 21600, null, null, false, false);
        $cookieJar->queue($cookie);

        $categories = Category::whereNull('parent_id')->with('subcategories')->get();

        if ($city->weather_id) {
            $weather = $this->getWeather($city->weather_id);
        } else {
            $weather = [
                'city' => '',
                'temp_min' => '',
                'temp_max' => '',
                'day' => '',
                'symbol' => '',
                'symbol_desc' => ''
            ];
        }

        return view('pages.categories', compact('categories', 'city', 'weather'));
    }

The problem is that if you go to the '/' root first time method will do it's work but when you're getting the city like /moskva and if you change your city with another for example /abakan and then go the root '/' you will be redirected to the /moskva. I tried almost everything, turned off any cahing, cleared cache with artisan commands but it does not helped. Here is domain for testing this issue: okololo.ru



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire