php - Can multiple routes access the same initialized object in laravel? - Stack Overflow

If I have initialized an object, somewhere, Idk how to do this, so, somewhere$sharedObject = new share

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744189456a4562362.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信