O
O
Onatskyi2019-06-16 04:56:17
PHP
Onatskyi, 2019-06-16 04:56:17

How is the app()->setLocale() function implemented?

Hello.
In Laravel, to change the language of the application, you need to run

App::setLocale();
// или
app()->setLocale();

So I was wondering how the second method is implemented and what are its pros and cons.
The function (or whatever it is called) can be accessed from anywhere, but the class must be connected every time through use. Tried to find information on the Internet, but without success. I would be grateful for any help and information on this matter. PHP I only study, but you can kick. :)
Using the poke method, this is how I implemented it:
class App
{
  protected static $instance = null;
  protected $locale = 'ru';

  public static function getIstance() {
    if ( self::$instance === null ) self::$instance = new self();
    return self::$instance;
  }

  public function __construct() {}
  public function __clone() {}

  public function setLocale( $locale )  {
    $this->locale = $locale;
  }
  public function getLocale( $locale )  {
    return $this->locale;
  }
}

function app() { return App::getIstance(); }

app()->getLocale(); // ru
app()->setLocale('en');
app()->getLocale(); // en

I'm not sure about this method, although it works.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
NubasLol, 2019-06-16
@NubasLol

Onatskyi , the first way uses facades that run on php magic
https://www.php.net/manual/en/language.oop5.magic.php
The second way uses global functions that are created via composer in the application file
App() gives access to laravel container
Read about dependency injection and service container. These are the patterns that are implemented in the lara
. And you tried to make a singleton, but the lara already has everything for this, you can put the necessary instances into the service container yourself.

S
sergey, 2017-08-01
@zorro76

opened the screen and got scared, what is this entry: section col-lg-12?
This is Bootstrap and there is no free creativity in it, open the tutorial and learn.
this is how you can write, and since you are not

<div class="container-fluid main-offer">
  <div class="row">
    <div class="col-sm-5 text-center description">
      <h1>gina</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam porro illum reiciendis magni cupiditate, blanditiis sapiente! Labore quo optio aliquam.</p>
    </div>
  </div

advice for the future: the code is provided not in screenshots, but in the sandbox: jsfiddle/codepen
example, but instead of color, put your image
jsfiddle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question