admin管理员组

文章数量:1399180

If I have initialized an object, somewhere, Idk how to do this, so, somewhere

$sharedObject = new sharedClass();

Supposing I have two routes in web.php:

Route::get('/page1', function(){
    $sharedObject->setFoo('newVal');
    return $sharedObject->getFoo();
});

Route::get('/page2', function(){
    return $sharedObject->getFoo();
});

So, is there a way to achieve this functionality, I just want to access the properties I had set in the original instantiation of an object, through another route?

If I have initialized an object, somewhere, Idk how to do this, so, somewhere

$sharedObject = new sharedClass();

Supposing I have two routes in web.php:

Route::get('/page1', function(){
    $sharedObject->setFoo('newVal');
    return $sharedObject->getFoo();
});

Route::get('/page2', function(){
    return $sharedObject->getFoo();
});

So, is there a way to achieve this functionality, I just want to access the properties I had set in the original instantiation of an object, through another route?

Share Improve this question asked Mar 25 at 14:28 GursimranjitGursimranjit 411 silver badge6 bronze badges 4
  • 3 "original instantiation of an object" - not 100% certain I know what you mean. First, you'd have to capture $sharedObject with use for your functions, although I don't know (one way or another) if that's the recommended pattern for Laravel. Second, you understand that the setFoo() in the first route won't have any effect on the getFoo() in the second route, right? – Chris Haas Commented Mar 25 at 14:43
  • 1 Are sessions not sufficient for this use case? Set a session variable in the first route, access it in the 2nd route (or perform some kind of logic if that value isn't set, etc.) – Tim Lewis Commented Mar 25 at 14:45
  • 2 HTTP requests in PHP/Laravel are stateless, and objects do not persist across routes automatically. Therefore, you need to use a session as suggested by @TimLewis , or you could employ less secure methods, which are not recommended. – Abdulla Nilam Commented Mar 26 at 5:52
  • Use Service Container – Nick Commented Mar 26 at 8:25
Add a comment  | 

1 Answer 1

Reset to default 3

I'm not sure if it will meet your requirements, but there are 2 ways to share the same object.

  1. Sessions
  2. Cache

Sessions are mostly used for saving logged in user's information for a short time (depends on the session lifetime setting).

Cache is used to store heavy computational data temporarily and can be immediately retrieved.

You can implement through the following:

use Illuminate\Http\Request;

// Session
Route::get('/page1', function(Request $request){
    $sharedObject = new SharedObject();
    $sharedObject->setFoo('newVal');
    $request->session()->put('sharedObject', $sharedObject);
    
    return $sharedObject->getFoo();
});

Route::get('/page2', function(Request $request){
    $sharedObject = $request->session()->get('sharedObject');
    return $sharedObject->getFoo();
});


// Cache
Route::get('/page1', function(Request $request){
    $sharedObject = cache('sharedObject');
    if (is_null($sharedObject)) {
        $sharedObject = new SharedObject();
        $sharedObject->setFoo('newVal');
        cache(['sharedObject' => $sharedObject], now()->addHour()); // store for an hour.
    }
    
    return $sharedObject->getFoo();
});

Route::get('/page2', function(Request $request){
    $sharedObject = cache('sharedObject');

    return $sharedObject->getFoo();
});

Just make sure that your $sharedObject can be serializable. I believe laravel will store the data as a serialized string.

本文标签: phpCan multiple routes access the same initialized object in laravelStack Overflow