Laravel 5.6不可解析的依赖解析

时间:2018-03-13 07:09:01

标签: laravel laravel-5 laravel-5.6

我正在尝试使用社交网站登录Twitter。 一切都很好,直到回调。一旦我到达回调,我就会遇到这个错误,在类Illuminate \ Support \ ServiceProvider

中称为无法解析的依赖项解析[参数#0 [$ app]]

以下是我的服务提供商:

<?php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use User;
use Laravel\Socialite\Contracts\User as ProviderUser;

class SocialTwitterAccountService extends ServiceProvider
{

    public function createOrGetUser(ProviderUser $providerUser)
    {
        $account = SocialTwitterAccount::whereProvider('twitter')
            ->whereProviderUserId($providerUser->getId())
            ->first();

        if ($account) {
            return $account->user;
        } else {

            $account = new SocialTwitterAccount([
                'provider_user_id' => $providerUser->getId(),
                'provider' => 'twitter'
            ]);

            $user = User::whereEmail($providerUser->getEmail())->first();

            if (!$user) {

                $user = User::create([
                        'email' => $providerUser->getEmail(),
                        'name' => $providerUser->getName(),
                        'password' => md5(rand(1, 10000)),
                ]);
            }

            $account->user()->associate($user);
            $account->save();

            return $user;
        }
    }
}

我还在提供商中注册了它:

App\Providers\SocialTwitterAccountService::class,

以下是我的重定向和回调函数:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use App\Providers\SocialTwitterAccountService;

class SocialAuthTwitterController extends Controller
{

    /**
     * Create a redirect method to twitter api.
     *
     * @return void
     */
    public function redirect()
    {
        return Socialite::driver('twitter')->redirect();
    }

    /**
     * Return a callback method from twitter api.
     *
     * @return callback URL from twitter
     */
    public function callback(SocialTwitterAccountService $service)
    {
        $user = $service->createOrGetUser(Socialite::driver('twitter')->user());
        auth()->login($user);
        return redirect()->to('/home');
    }
}

0 个答案:

没有答案