Jump to content

Can a PHP model call a view?


DeX
 Share

Recommended Posts

Right now in my code I have a page that displays a quote (like from a mechanic) and the quote is built in the quoteView. As the quote is displayed on screen, I have some code being called to create a PDF of the quote and save it on the server. So after the call to fetch the quote is made, I call the controller to create the PDF, the controller calls the quoteModel to create it, but the model needs to call the view to build the quote so it can be saved. Should the model call back to the view? Should I just pass in the HTML for the quote from view->controller->model?

 

The major issue I have is all views share the same controller so I need to pass that in when creating the view, but the model doesn't have any concept of the controller. I'm still learning this, thanks.

Link to comment
Share on other sites

No. In the simple version of MVC, as it applies to PHP, models should never talk to views, and the most they do with controllers is return data that is asked of them.

 

1. Request comes to the controller to generate the PDF

2. Controller retrieves the necessary data from the model

3. Controller passes the data to the view

4. View generates and outputs the PDF

 

It's just like any other request lifecycle except this time you're showing a PDF instead of HTML.

 

The major issue I have is all views share the same controller so I need to pass that in when creating the view,

Well that's not right. A controller represents a distinct type of page. For example, the "view a topic" page. The controller has logic for that particular page and also decides which view should be rendered as the response. For example, most of the time the "view a topic" controller will show the "view this particular topic" view, but sometimes it might need the "no such topic" view or the "access denied to this topic" view.

 

Having all views go through the same controller is crazy. If you have a lot of common work involved in most/all pages then you should put the shared logic into an abstract base class and have the normal controllers extend from it.

 

but the model doesn't have any concept of the controller.

Correct.
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.