tombenner / wp-mvc

An MVC framework for WordPress
http://wpmvc.org
MIT License
624 stars 172 forks source link

"After actions" don't run when I use render_view in an action of a controller #230

Closed nhatnx closed 5 years ago

nhatnx commented 6 years ago

I want to use custom layout in an action of a controller, so I use render_view in it. But if I use render_view, "after actions" won't be executed. I find that you use "die()" in render_view function in mvc_controller.php. I think it prevents "after actions" from continuing running.

cyberscribe commented 6 years ago

After the view is rendered, the execution is complete. I'm not sure what you mean by "after actions". The only such things I know about are in models, not controllers or views (e.g. "after find").

On Fri, 9 Nov 2018 at 04:09, Nhat Nguyen notifications@github.com wrote:

I want to use custom layout in an action of a controller, so I use render_view in it. But if I use render_view, "after actions" won't be executed. I find that you use "die()" in render_view function in mvc_controller.php. I think it prevents "after actions" from continuing running.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tombenner/wp-mvc/issues/230, or mute the thread https://github.com/notifications/unsubscribe-auth/AAqV5bF-q72sNjiQwTQOGMLaUG8bbkYKks5utP_vgaJpZM4YV7dd .

nhatnx commented 6 years ago

I mean this one http://wpmvc.org/documentation/controllers/controller_attributes/after.html

If you have an action with $this->render_view(...) in it, "after" methods of this controller won't be executed. E.g. set_venues() in above link.

cyberscribe commented 6 years ago

This will run before the view is rendered.

On Fri, 9 Nov 2018 at 09:07, Nhat Nguyen notifications@github.com wrote:

I mean this one http://wpmvc.org/documentation/controllers/controller_attributes/after.html

If you have an action with $this->render_view(...) in it, "after" methods won't be executed. E.g. set_venues() in above link.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tombenner/wp-mvc/issues/230#issuecomment-437296048, or mute the thread https://github.com/notifications/unsubscribe-auth/AAqV5RrBfuLWGMZXjx2sw6IQOCDHrVHsks5utUXQgaJpZM4YV7dd .

nhatnx commented 6 years ago

No. You can try:

class EventsController extends MvcPublicController {

  var $after = array('set_venues');

  public function set_venues() {
    // do something here after executing an action
  }

  public function some_action() {
    $this->render_view('show', array('layout' => 'custom_layout'));
  }

}

Then you call events/some_action/ set_venues() won't be reached.

cyberscribe commented 6 years ago

Ah, right. It only gets fired I guess if you let the controller "fall through" to the view of the same name; not if you explicitly call render_view.

If you think this is a bug, we would welcome a pull request: https://github.com/tombenner/wp-mvc

On Fri, 9 Nov 2018 at 09:15, Nhat Nguyen notifications@github.com wrote:

No. You can try: load_model('Venue'); $venues = $this->Venue->find(); $this->set('venues', $venues); } public function some_action() { $this->render_view('show', array('layout' => 'custom_layout')); } } ?>

Then you call events/some_action/ set_venues() won't be reached.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tombenner/wp-mvc/issues/230#issuecomment-437298111, or mute the thread https://github.com/notifications/unsubscribe-auth/AAqV5aYGqQUStXe4mULOlTw9rZZAKMjKks5utUeigaJpZM4YV7dd .

nhatnx commented 6 years ago

But how can I use custom layout without using render_view() in controller?

cyberscribe commented 6 years ago

You can't.

But you can just call:

$this->set_venues() $this->render_view(...

in any controller endpoint where you want that function to execute before rendering the view.

On Fri, 9 Nov 2018 at 09:40, Nhat Nguyen notifications@github.com wrote:

But how can I use custom layout without using render_view() in controller?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tombenner/wp-mvc/issues/230#issuecomment-437304868, or mute the thread https://github.com/notifications/unsubscribe-auth/AAqV5R7dlCny6P-a_rc0h_QRlTTAqYPcks5utU2NgaJpZM4YV7dd .

nhatnx commented 6 years ago

Ah, I would like to do something more (not only set_venues()) after rendering the view with custom layout 😄

cyberscribe commented 6 years ago

Ah, right.

Well, the controller context is available to you in the view ($this), so you should be able to trigger additional code at the bottom of the layout if desired.

Again, if you see a better structure or approach here, feel free to submit a pull request.

Good luck!

On Fri, 9 Nov 2018 at 14:13, Nhat Nguyen notifications@github.com wrote:

Ah, I would like to do something more (not only set_venues()) after rendering the view with custom layout 😄

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tombenner/wp-mvc/issues/230#issuecomment-437371178, or mute the thread https://github.com/notifications/unsubscribe-auth/AAqV5XXV3pnCMi6I23I2FEabugbrKtuuks5utY2VgaJpZM4YV7dd .