4

I'm using Laravel to create a complex registration form. Now I want to use the Company Object throughout the whole process.

I'm currently creating a new Company object in the constructor as follows:

public function __construct() { $this->company = new Company(); } 

And then I fill the object with data from an API.

But the object is returning null every time I hit a new method or route.

I don't see any other way of going about this? Any idea's? Thanks!

The following is working

return redirect()->route('register.contacts')->with('company', $company); 

And in the contacts method

public function contacts() { Session::get('company')); } 
11
  • 1
    Which class's constructor is this? And what exactly is your intention? Commented Jul 3, 2015 at 8:01
  • My intention is to pass an object to other methods in my controller, but it seems to lose the global objects every time I hit a anoter route Commented Jul 3, 2015 at 8:02
  • You can use dependency injection. Commented Jul 3, 2015 at 8:07
  • 1
    @Notflip you don't have much of a choice so I'd stick with storing it in the session. You have to realize that PHP applications are request based. After each request, all data in memory is lost. You only keep things that you store in the database, the session or the filesystem in general. Commented Jul 3, 2015 at 8:15
  • 1
    When you redirect to another route you don't call another controller method. You send a response to the client that tells him to what URL the next request should go. The browser then calls that route. By the way, dependency injection has nothing to do with this... Commented Jul 3, 2015 at 8:27

1 Answer 1

4

Stick with what works, there isn't really a better alternative. You have to realize that PHP applications are request based. After each request, all data in memory is lost. You only keep things that you store in the database, the session or the filesystem in general.

Also when you redirect to another route, you don't call another controller method. You send a response to the client that tells him to what URL the next request should go. The browser then calls that route. Those requests are completely decoupled, so the only way you can pass data is in the URL (that doesn't work that well for full objects) or somehow storing it on the server and loading that data again on the subsequent request.

Just for the sake of completeness, here are two alternatives:

  • Save the not-yet-complete model to the database. Tricky to avoid "dead body records" though
  • Change your register process to be all done client side and sent in one go to the server.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.