yii2 doesnot store Cookie in localhost or server

I write this below code in a different controller to store cookies

         $cookies = Yii::$app->response->cookies;
        $number=0;
        if($cookies->has('registration_id')){
            if($cookies->has('registration_attempt')){
                $cookie_registration_att =               $cookies->getValue('registration_attempt');
                $number = registration_att+1;
                $cookies->add( new Cookie([
                    'name' => 'registration_attempt',
                    'value' => $number,
                    'expire' => time() + 86400*2,
                ]));
            }
            else{
                $cookies->add( new Cookie([
                    'name' => 'registration_attempt',
                    'value' => $number,
                    'expire' => time() + 86400*2,
                ]));
            }

        } else{



            $cookies->add(new Cookie([
                'name' => 'registration_id',
                'value' => 'Generate Id',
                'expire' => time() + 86400*2,
            ]));
            $cookies->add( new Cookie([
                'name' => 'registration_attempt',
                'value' => $number+1,
                'expire' => time() + 86400*2,
            ]));

         //**show here**
        }

I set secure true but it does not show anything in different controller . When i am trying to access cookies value it shows null. Cookie value set but it doesn’t set globally . Instant show cookie value where I commented show here. I am checking here if cookie set then if either else and always hit that. And if echo in that place it show data .