更改Laravel 4 Guard类的功能

时间:2015-05-05 13:46:25

标签: php laravel laravel-4

我正在尝试编写SSO实现,为此我需要覆盖Auth::check()类中实现的Guard等方法。

但是,我不了解如何使用服务提供商扩展该类。我试着查看AuthServiceProvider,但是有很多关于我不明白的傻瓜大肆宣传。

1 个答案:

答案 0 :(得分:0)

我明白了!相当简单:

<?php

namespace Animekyun\Providers;

use Animekyun\Auth\CustomGuard;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Support\ServiceProvider;

class SsoServiceProvider extends ServiceProvider
{

    public function boot()
    {
        \Auth::extend('custom', function () {
            return new CustomGuard(
                new EloquentUserProvider(
                    $this->app['hash'],
                    $this->app['config']['auth.model']),
                $this->app['session.store']);
        });
    }

    public function register()
    {
    }
}

CustomGuard类:

<?php
namespace Animekyun\Auth;


use Illuminate\Auth\Guard;

class CustomGuard extends Guard
{
    public function check() {
        // do some stuff
        return parent::check();
    }
}