How do get Phalcon Session from the DI in a standalone class - session

I have a class that I have created in my Phalcon project. I want to get the session service from the DI so that I can check if a variable is set.
I have tried:
$di = Phalcon\DI::getDefault();
echo $di->get("session")->get("user_id");
exit;
However it comes back saying A session had already been started - ignoring session_start()
use Phalcon\Mvc\Model\Resultset\Simple, Phalcon\Mvc\User\Plugin;
class JsonReturnObject extends Plugin
My session service is registered like this:
<?php
use Phalcon\Mvc\View;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaData;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Flash\Session as FlashSession;
use Phalcon\Events\Manager as EventsManager;
//setup the dependency injection
$di = new FactoryDefault();
//setup the session
$di->set('session', function() {
$session = new SessionAdapter();
$session->start();
return $session;
});
Any help would be appreciated.

Try registering the session as not a shared component like this:
$di->set(
'session',
function () {
$session = new \Phlacon\Session\Adapter\Files();
$session->start();
return $session;
}
);
Note the third parameter is not true on the $di->set()
Alternatively use $di->set() instead of $di->setShared()

The fix was to set the service to shared using setShared.

Related

Saving an object into the session or cookie

I'm using Instagram API library to connect user to Instagram profile and then do smth with it. So, as Instagram API wiki says:
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.
I have initialized InstagramAPI class and then I called $ig->login('username', 'password');. But I have to call it in every function where I need to work with Instagram.
So how could I save this object $ig for using it in the future in other controllers without calling login() any more? Can I save $ig object into the session or cookie file?
P.S. I think saving into the session is not safe way to solve the issue.
UPD: I was trying to save $ig object into the session, however the size is large and session become stop working as well.
Regarding the register method you asked in the comments section, all you need to create a new service provider class in your app\providers directory and declare the register method in there for example:
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();
});
}
}
Then, add your newly created InstagramServiceProvider class in providers array inside the config/app.php file for example:
'providers' => [
// Other ...
App\Providers\InstagramServiceProvider::class,
]
Now on, in any controller class, whenever you need the Instagram instance, all you need to call the App::make('InstagramAPI\Instagram') or simply call the global function app('InstagramAPI\Instagram') or even you can typehint the class in any method/constructor etc. Some examples:
$ig = App::make('InstagramAPI\Instagram');
$ig = App::make(Instagram::class); // if has use statement at the top fo the class
$ig = app('...');
In a class method as a dependency:
public function someMethod(Instagram $ig)
{
// You can use $ig here
}
Hope this helps but read the documentation properly, there will get everything documented.

How to use global variables in Laravel

Is it possible to share and change some variable between multiple views? For example, I want to have a variable $user that will be shared between all views. When a user logs the variable is set up, when the user logs out, the variable is unset. I was unable to achieve requested using
the following combination:
in AppServiceProvider:
view()->share('var', 1);
in the controller:
$var = view()->shared('var');.
$var ++;
view()->share('var', var);
return view(''', 'var'=>$var)
Every time when the page is reloaded $var is always the same (2).
I want to have a variable $user that will be shared between all views
You should use auth()->user() to get authenticated user instance in any view.
But if you don't want to use it for some reason, you could share the variable between multiple views with a view composer.
share() method will be useful only if you want to share a variable with all views. To make it work, put view()->share('key', 'value') to the boot() method of a service provider.
Also, the code in your controller looks like you want to share data not between views, but between requests. Use session for that.
To save the data:
session(['key' => 'value']);
To get the data in another request:
session('key');
It would be better to add another service provider. Take a look at my provider:
<?php
namespace App\Providers;
use Request;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider
{
public function boot()
{
$this->globalThings();
//call another globals' function here
}
public function register()
{
//
}
/**
* Get the golbals
*/
private function globalThings()
{
view()->composer(array('*.*'),function($view){
//get the data however you want it!
$view->with('global', Model::where('field','value')->get());
});
}
And don't forget to add the service provider to list of provider is config/app.php
App\Providers\ViewComposerServiceProvider::class,

Laravel: Inject Auth into a Service

How do I inject Auth into a service? I know in the service itself, I do:
public function __construct(Illuminate\Auth\AuthManager $Auth)
{
$this->authService = $Auth;
}
But how do I pass it in the service provider? I.e.
$this->app->bind('SomeService', function() {
return new SomeService(??); // What goes here?
});
I believe you should allow the laravel app to build an object based on your dependency here. I haven't tried this myself but looks like this might work:
return new SomeService(App::make(Illuminate\Auth\AuthManager));

Overriding the login() method of Sentry 2 package in Laravel 4

My basic aim is to extend the package class and override a method in it.
I have used Fnatte's ans as a reference : How to extend laravel 4 core?
Sentry2 is a package that i am using for authentication in larval 4.
A user can be logged in using Sentry::login($credentials)
I want to override the login method of the Sentry package and remove the check for activating the user(i have commented it in the code below)
public function login(UserInterface $user, $remember = false)
{
#prevent throwing error if not activated !
// if ( ! $user->isActivated())
// {
// $login = $user->getLogin();
// throw new UserNotActivatedException("Cannot login user [$login] as they are not activated.");
// }
$this->user = $user;
// Create an array of data to persist to the session and / or cookie
$toPersist = array($user->getId(), $user->getPersistCode());
// Set sessions
$this->session->put($toPersist);
if ($remember)
{
$this->cookie->forever($toPersist);
}
// The user model can attach any handlers
// to the "recordLogin" event.
$user->recordLogin();
}
Steps i have done till now :
1. Created a app/lib folder and added my extension class CustomSentry in it.
2. Added the app/lib folder to composer.json class map
app/lib/CustomSentry.php :
use Cartalyst\Sentry\Sentry;
use Cartalyst\Sentry\Users\UserInterface;
class CustomSentry extends Sentry{
public function login(UserInterface $user, $remember = false){
$this->user = $user;
// Create an array of data to persist to the session and / or cookie
$toPersist = array($user->getId(), $user->getPersistCode());
// Set sessions
$this->session->put($toPersist);
if ($remember)
{
$this->cookie->forever($toPersist);
}
// The user model can attach any handlers
// to the "recordLogin" event.
$user->recordLogin();
}
}
4. Created a service provider app/lib/CustomSentryServiceProvider
use Cartalyst\Sentry\SentryServiceProvider
class CustomSentryServiceProvider extends SentryServiceProvider{
//What should i put it here?
}
5. Register the service provider in app/config/app.php
'CustomSentryServiceProvider'
6. Use it as :
Sentry::login($credentials);
Well i figured out the answer to the question.
The better way to extend the class would be to use the same name. Since i am already using namespacing it would help reduce the confusion.
Assuming my apps name is MyApp i will only need to replace:
Cartalyst\Sentry
by:
MyApp\Cartalyst\Sentry
the rest of the SentryService provider can be copied as it is.
Then i can call the login method the normal way
Sentry::login($credentials)
I have doubts, because you have to copy whole package/Sentry. Did you tried to extend the Sentry class? This this the best way to do what you want to achieve.

laravel-4 way to inject an object that requires configuration into a controller

I would like to find a good way to pass a pre-configured object to a controller. I know that I can use the IoC like below:
Mycontroller extends extends \Illuminate\Routing\Controllers\Controller {
//i can only use one config uless i pass Request data
$this->config = App::make('MyconfigObject');
}
but this seems to have the limitation of only being able to use one config. I would rather do something like the following:
Route::get('some-route', function()
{
$config = Config::get('some.config');
$object = new MyConfigObject($config);
Route::dispatch(MyController($object));
});
The reason I would like to do this is that I would like to dispatch the same controller, but with different configuration for several routes.
I'm not totally satisfied with this method but its the best I've come up with so far, using the IoC's automatic resolution.
bootstrap/stat.php
/*
* bindings to the IoC container
*/
$app->singleton('MyNamespace\Transfer\TransferStategyInterface', function() {
$config = Config::get('transfer-strategy');
return new LocalTransferStrategy($config);
});
use MyNamespace\Transfer\TransferStategyInterface;
TransferController.php
use MyNamespace\Transfer\TransferStategyInterface;
class TransferController extends BaseController {
protected $transferStrategy;
public function __construct(TransferStategyInterface $transferStrategy = null)
{
$this->transferStrategy = $transferStrategy;
}
}

Resources