I'm developing my own PHP MVC for a database application, which I'd already completed but using monolithic classes that do everything - MySQL processing, input processing and display. It works great but it's a nightmare to debug and to make alterations! I've recently discovered MVC's and in the process of rewriting the app - it's all on the drawing board at the moment so I haven't actually got any code to share with you.
There's one thing I'm stumbling on: I've got it that the Controller should call the Model to modify it based on user input, and then call the View, which will access the Model(s) it needs to get its data and then display it. I'd like the view to gets its data independently of the Controller, and not have the Controller act as the intermediary. I started off down that line of the Controller getting its data from the Model and handing it to the View, but quickly ran into trouble where a View would need access to several Models.
To explain what I'm stuck on, I'll draw an example:
Example: submitting a form with data, which fails the Model's validation
Front controller:
- Load the relevant controller and call the ProcessForm action
Controller:
- Instantiate the Model class and call the methods to load and validate the data
Model:
- validation failed: generate a list of errors and return 'false'
Controller:
- If Model had returned 'true', redirect to the Index page
- It returned 'false': invoke the View
View:
- Instantiate the Model class and fetch the data
- Display the data and the error message(s)
My problem is, how can the View, which has its own instance of the Model, get the error message(s) which were generated in the Controller's instance of the model, without the Controller handing its own Model instance directly to the View? I'm figuring the first instance would need to store the messages somewhere where the second instance could retrieve them?
I realise the first instance could return its errors to the Controller, which it can then pass to the view, independent of the Model itself, but I'd still like to keep the View independent of the Controller.