2015-10-27 96 views
5

我已经改性Laravel的放置在自定义功能添加到

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

供应商文件验证类Laravel(扩展保护类)

,但在更新Laravel时将被覆盖。

我正在寻找一种方法将代码放在我的/ app的某处,以防止覆盖。

修改功能是

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

也有针对该文件的一个新功能:以上

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

代码被称为在我的应用程序的许多地方。

有什么想法?

+0

那整个应用程序的类。你不应该直接搞乱Laravel的源代码。 –

回答

6

步骤:
由1- AppServiceProvider创建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或新的服务提供商或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-在config/auth中。 PHP

'driver' => 'customAuth', 

4-现在如果你想覆盖你应该创建一个扩展卫队类和* *,然后重写方法的类的方法,并使用您可以使用此

Auth::type(); 
+0

MyGuard应该如何加入?我有一个“扩展”文件夹。但是用“/使用App \ Extensions \ MyGuard;”在AppServiceProvider中找不到。 在MyGuard文件中,我有“使用Illuminate \ Auth \ SessionGuard; 使用Illuminate \ Contracts \ Auth \ Guard;”但我也不确定。 – Olivvv

0

这看起来并不像你需要更新Guard。据我所见,你只是试图从会话中检索数据。对于卫队本身来说这绝对不是事。

你自己已经访问会话的多种方式:

// 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 
相关问题