【问题标题】:How to create global object Laravel如何创建全局对象 Laravel
【发布时间】:2018-05-14 18:15:41
【问题描述】:

我正在使用 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)),我发现以前的数据没有保存。 “WTF?” - 我说

    有人告诉我 singleton 只是向我保证不会创建相同的对象,但它不会保存任何更改。

  • 我尝试使用sessions

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

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

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


请告诉我:

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

【问题讨论】:

  • 您确定您使用的是正确的存储空间吗,check this
  • 那里说:如果你想使用mySQL存储...IF 但是,我不想使用 mysql :D
  • 你是如何设置单例的?单身人士应该可以解决这个问题,所以我想知道它是否没有正确注册。
  • @jfadich,更新问题。我不明白为什么对象的属性没有改变..?
  • 我就是这么想的。您想在单例寄存器调用中调用$ig->login('name', 'pass')。函数的结果是传递给函数的结果,您在注入后所做的任何更改都不会传播回容器中。

标签: php laravel instagram-api


【解决方案1】:

当我以任何方法调用 $ig->login('name', 'pass') 时,此对象中的所有用户配置文件信息都发生了变化,但是如果我调用 dd($ig = app(Instagram::class))在另一个控制器方法中,我看到以前的数据没有保存。

这是正确的行为。当一个新的请求被发送到 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。然后,可以在您的控制器中使用 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);
    }
}

【讨论】:

  • 我过几天试试,谢谢!如果一切都好,我选择你的答案是最好的:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
相关资源
最近更新 更多