-
-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow $data to be passed to view model #39
Allow $data to be passed to view model #39
Conversation
I've been injecting the request into the VM's constructor to allow this behaviour. What's the benefit of this approach? |
I think both approaches are valid. I personally like to DI the VM into the Controller params at the moment, it's not possible to pass data into the view when using this method. I felt that this would allow for a more consistent controller by allowing this instead of some being newed up vs DI. |
I'm not convinced about it… I prefer one way of doing things, and personally like the solution with less magic better. I appreciate you thinking along, but I'm afraid I'll close this one for now. |
Hi @brendt, Would you be able to reconsider this, the more I use this package the more I feel this is massively missing. Imagine this situation: We have 3 admin areas:
Each of these panels has a BaseView model that gets called in the controller I.e. All of these panels, share the same code when it comes to authentication, I just use inheritance to change the guard based on what's going on. class AdminMFASetupController extends BaseMFASetupController
{
public function getViewModel()
{
return AdminMFASetupViewModel::class;
}
public function getGuard()
{
return 'admin';
}
} abstract class BaseMFASetupController
{
public function index()
{
//...
$qrURL = QRGenerate::run();
$viewModel = new ($this->getViewModel())(qrURL: $qrURL);
return $viewModel->view('some-view');
}
} In this case, I would need to create a ViewModel for every instance of our Panel which has this Setup, It would be a lot cleaner to be able to use the public function index()
{
$qrURL = QRGenerate::run();
return (new $this->getViewModel())->view('some-view', ['qrURL' => $qrURL]);
}
public function getViewModel()
{
return AdminViewModel::class;
} Thoughts? |
Thanks for the example, I believe I misunderstood your first example because of the request/response injection. Passing data to the view makes a lot more sense. I've still got one reservation though with:
Yes, that was kind of my design goal: a dedicated view model for every view. Personally I wouldn't choose the level of abstraction you're introducing with the Apart from that, I think I'm fine merging this in. Let me ask around for some opinions at Spatie, and I'll get back to it. |
Hey @brendt, have you guys had any more thoughts on this? |
This PR has broken the use of the property Given a ViewModel:
The response is:
I'm not aware if it's following semver, but a major version bump would have been nice for composer compatibility. I'm just raising awareness since its a pretty common key to use in API responses. |
Interesting, I'll look into this now. |
@asurcodes Just sent a bug fix PR in to fix this issue moving forward. |
Fix is tagged in 1.5.1: https://github.com/spatie/laravel-view-models/releases/tag/1.5.1 |
This PR allows you the ability to pass data into the ViewModel from the Controller, I thought this was useful if you are passing certain parts of the request into the view.