gpt4 book ai didi

php - 如何创建全局对象 Laravel

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:27 26 4
gpt4 key购买 nike

我正在使用 Laravel 5.6Instagram API library .

要使用此 Instagram API,我需要创建对象 $ig = new\InstagramAPI\Instagram()。然后,为了获取任何用户的信息,我必须每次都使用 $ig->login('username', 'password')

所以我不想一直使用这个功能。首先,我想创建一个包含 $ig = new\InstagramAPI\Instagram() 的全局变量。但是,我不知道如何正确地做到这一点。

  • 我尝试使用 singleton :

    $this->app->singleton(Instagram::class, function ($app) {
    Instagram::$allowDangerousWebUsageAtMyOwnRisk = true; // As wiki says
    return new Instagram();
    });

    当我在任何方法中调用 $ig->login('name', 'pass') 时,此对象中所有用户配置文件的信息都会更改,但是如果我调用 dd($ ig = app(Instagram::class)) 在另一个 Controller 方法中,我看到以前的数据没有保存。 “WTF?” - 我说

    Someone tells me that singleton just promise me that there won't be created the same object, but it does not save any changes.

  • 我尝试使用 sessions :

    但是,当我尝试将带有对象的变量设置为值时,任何事情都没有发生。

    $ig = new \InstagramAPI\Instagram();
    session(['ig' => $ig]);

    我认为这是因为我试图放置一个大物体。 另一方面,这不是安全的方法!


请告诉我:

如何创建一个对象,我可以在每个方法中使用该对象并为接下来的操作保存更改?

最佳答案

When I called $ig->login('name', 'pass') in any method all user profile's information changed in this object, but then if I call dd($ig = app(Instagram::class)) in another Controller method I see that previous data did not save.

这是正确的行为。当向 Laravel 发送新请求时,将创建一个新的 Instagram 实例。我不确定您是否理解单例的含义,但就 Laravel 而言,每个 HTTP 请求都有一个实例。

由于您使用的 Instagram API 不包含重新登录的功能,因此我创建了一个类(将放置在 app/Classes 文件夹中)。

<?php
namespace App\Classes;

use InstagramAPI\Instagram;
use InstagramAPI\Response\LoginResponse;

class CustomInstagram extends Instagram {
public function relogin(LoginResponse $response) {
$appRefreshInterval = 1800;

$this->_updateLoginState($response);
$this->_sendLoginFlow(true, $appRefreshInterval);

return $this;
}
}

更改单例实例,使其使用 App\Classes\CustomInstagram 类。

$this->app->singleton(Instagram::class, function ($app) {
Instagram::$allowDangerousWebUsageAtMyOwnRisk = true; // As wiki says
return new App\Classes\CustomInstagram();
});

为了通过身份验证用户使用 Instagram 对象,需要以某种方式保留登录信息。这将放置在登录发生的地方。

try {
$response = app(Instagram::class)->login($username, $password);

if ($response->isTwoFactorRequired()) {
// Need to handle if 2fa is needed (we're not completely logged in yet)
}

// Can use session to persist \InstagramAPI\Response\LoginResponse but I'd recommend the database.
session(['igLoginResponse' => serialize($response)]);
} catch (\Exception $e) {
// Login failed
}

然后创建一个中间件以将用户重新登录到 Instagram(如果存在登录响应)。您需要将其注册为 described here .然后,可以在您的 Controller 中使用 Instagram 单例。

namespace App\Http\Middleware;

use Closure;
use InstagramAPI\Instagram;

class InstagramLogin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$responseSerialized = session('igLoginResponse');

if (!is_null($responseSerialized)) {
$ig = app(Instagram::class);

$response = unserialize($responseSerialized);

$ig->relogin($response);
}

return $next($request);
}
}

关于php - 如何创建全局对象 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50336606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com