A
A
Analka2020-12-15 14:10:43
Laravel
Analka, 2020-12-15 14:10:43

How to make a global variable?

How can I make the value the same for all pages?

The class that generates the captcha value

class MathCaptcha
{
  private $firstNumber;
  private $secondNumber;
  private $name = 'result_captcha';

  public function __construct(int $firstNumber,int $secondNumber, string $name = null)
  {
    $this->firstNumber = $firstNumber;
    $this->secondNumber = $secondNumber;
    if(!empty($name)) $this->name = $name;
  }

  public function getResult(): string
  {
    $time = time();

    $first = random_int(0,$this->firstNumber);
    $second = random_int(0,$this->secondNumber);

    if($time & 1){
      $str = $first.'-'.$second;
      $result = $first - $second;
    }else{
      $str = $first.'+'.$second;
      $result = $first + $second;
    }
    
    session([$this->name => $result]);

    return $str;
  }
}

I do this in the service provider
public function register()
    {
      View::composer(['*'], function ($view) {
        $captcha_register = (new MathCaptcha(20, 9,'captcha_register'))->getResult(20, 9);

        $view->with([
          'captcha_register' =>  $captcha_register,
        ]);
      });
    }

but it turns out for the default template one value, for the registration page another

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question