Add a user-defined function to a class of the Larvel class (extends the protection class)

I changed the Laravel provider file placed in

/vendor/laravel/framework/src/Illuminate/Auth/Guard.php

but it will be overwritten when updating Laravel.

I am looking for a way to put the code somewhere in my / application to prevent overwriting.

Modified Function:

public function UpdateSession() { $this->session->set('type', $type); //==> Set Client Type } 

There is also a new function in the file:

 public function type() { return $this->session->get('type'); //==> Get Client Type } 

The codes above are called in many places in my application.

Any idea?

+5
source share
2 answers

Steps:
1- create myGuard.php

 class myGuard extends Guard { public function login(Authenticatable $user, $remember = false) { $this->updateSession($user->getAuthIdentifier(), $user->type); if ($remember) { $this->createRememberTokenIfDoesntExist($user); $this->queueRecallerCookie($user); } $this->fireLoginEvent($user, $remember); $this->setUser($user); } protected function updateSession($id, $type = null) { $this->session->set($this->getName(), $id); $this->session->set('type', $type); $this->session->migrate(true); } public function type() { return $this->session->get('type'); } } 

2- in AppServiceProvider or a new service provider or routes.php:

 public function boot() { Auth::extend( 'customAuth', function ($app) { $model = $app['config']['auth.model']; $provider = new EloquentUserProvider($app['hash'], $model); return new myGuard($provider, App::make('session.store')); } ); } 

3- in config / auth.php

 'driver' => 'customAuth', 

4- now you can use this

 Auth::type(); 
+6
source

This is not like updating the Guard. As far as I can see, you are only trying to extract data from the session. And it definitely has nothing to do with the Sentinel.

You have several ways to access the session yourself:

 // via Session-Facade $type = Session::get('type'); Session::put('type', $type); // via Laravels helper function $type = session('type'); // get session()->put('type', $type); // set session(['type' => $type']); // alternative 
0
source

Source: https://habr.com/ru/post/1234604/


All Articles