将对象保存到会话或 cookie 中
Saving an object into the session or cookie
我正在使用 Instagram API library 将用户连接到 Instagram 个人资料,然后对其进行处理。因此,正如 Instagram API wiki 所说:
Once you have initialized the InstagramAPI class, you must login to an account.
$ig = new \InstagramAPI\Instagram();
$ig->login($username, $password); // Will resume if a previous session exists.
我已经初始化了 InstagramAPI class 然后我调用了 $ig->login('username', 'password');
。但是我必须在需要使用 Instagram 的每个函数中调用它。
那么我怎样才能保存这个对象 $ig
以便将来在其他控制器中使用它而无需再调用 login()
?我可以将 $ig
对象保存到会话或 cookie 文件中吗?
P.S。我认为保存到会话中不是解决问题的安全方法。
UPD:我试图将 $ig
对象保存到会话中,但是大小很大,会话也停止工作。
关于您在评论部分询问的register
方法,您只需在app\providers
目录中创建一个新的service provider class并声明register
方法,例如:
namespace App\Providers;
use InstagramAPI\Instagram;
use Illuminate\Support\ServiceProvider;
class InstagramServiceProvider extends ServiceProvider
{
public function register()
{
// Use singleton because, always you need the same instance
$this->app->singleton(Instagram::class, function ($app) {
return new Instagram();
});
}
}
然后,将新创建的 InstagramServiceProvider
class 添加到 config/app.php
文件内的 providers
数组中,例如:
'providers' => [
// Other ...
App\Providers\InstagramServiceProvider::class,
]
现在,在任何控制器 class 中,只要您需要 Instagram
实例,只需调用 App::make('InstagramAPI\Instagram')
或简单地调用全局函数 app('InstagramAPI\Instagram')
甚至你可以 typehint
任何 method/constructor 中的 class 等等。一些例子:
$ig = App::make('InstagramAPI\Instagram');
$ig = App::make(Instagram::class); // if has use statement at the top fo the class
$ig = app('...');
在作为依赖项的 class 方法中:
public function someMethod(Instagram $ig)
{
// You can use $ig here
}
希望这对您有所帮助,但请正确阅读 documentation,它将记录所有内容。
我正在使用 Instagram API library 将用户连接到 Instagram 个人资料,然后对其进行处理。因此,正如 Instagram API wiki 所说:
Once you have initialized the InstagramAPI class, you must login to an account.
$ig = new \InstagramAPI\Instagram(); $ig->login($username, $password); // Will resume if a previous session exists.
我已经初始化了 InstagramAPI class 然后我调用了 $ig->login('username', 'password');
。但是我必须在需要使用 Instagram 的每个函数中调用它。
那么我怎样才能保存这个对象 $ig
以便将来在其他控制器中使用它而无需再调用 login()
?我可以将 $ig
对象保存到会话或 cookie 文件中吗?
P.S。我认为保存到会话中不是解决问题的安全方法。
UPD:我试图将 $ig
对象保存到会话中,但是大小很大,会话也停止工作。
关于您在评论部分询问的register
方法,您只需在app\providers
目录中创建一个新的service provider class并声明register
方法,例如:
namespace App\Providers;
use InstagramAPI\Instagram;
use Illuminate\Support\ServiceProvider;
class InstagramServiceProvider extends ServiceProvider
{
public function register()
{
// Use singleton because, always you need the same instance
$this->app->singleton(Instagram::class, function ($app) {
return new Instagram();
});
}
}
然后,将新创建的 InstagramServiceProvider
class 添加到 config/app.php
文件内的 providers
数组中,例如:
'providers' => [
// Other ...
App\Providers\InstagramServiceProvider::class,
]
现在,在任何控制器 class 中,只要您需要 Instagram
实例,只需调用 App::make('InstagramAPI\Instagram')
或简单地调用全局函数 app('InstagramAPI\Instagram')
甚至你可以 typehint
任何 method/constructor 中的 class 等等。一些例子:
$ig = App::make('InstagramAPI\Instagram');
$ig = App::make(Instagram::class); // if has use statement at the top fo the class
$ig = app('...');
在作为依赖项的 class 方法中:
public function someMethod(Instagram $ig)
{
// You can use $ig here
}
希望这对您有所帮助,但请正确阅读 documentation,它将记录所有内容。