Laravel项目中的路由或缓存

时间:2017-09-12 07:59:30

标签: php laravel-5

我有一个Laravel 5.4项目。有一个我无法在第二天解决的路线问题。 我有两条路线,如:

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://freegeoip.net/json/' . $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);
    }

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'));
    }

问题在于,如果你去'/'根第一次方法会做它的工作,但当你得到像/ moskva这样的城市,如果你改变你的城市,例如/ abakan,然后去root'/'你将被重定向到/ moskva。我尝试了几乎所有的东西,关闭任何cahing,用工匠命令清除缓存,但它没有帮助。 以下是测试此问题的域名:okololo.ru

0 个答案:

没有答案