Author Topic: something like preExec/postExec action  (Read 7516 times)

sylvek

  • Newbie
  • *
  • Posts: 6
    • View Profile
something like preExec/postExec action
« on: October 16, 2010, 05:21:20 am »
Hi everyone, I'm new on the forum and to the TinyMVC too.

I'm wondering is there something like preExec action (name from framework that I work on in company where I work), by that I mean action that is executed just before the requested action.
For example, I have to do something in every action in controller (load language, parse a file, it really doesn't matter) and I would like to have such action that I can do stuff in it, so I don't have to multiply the code in every action.

And same thing about postExec - action executed after the requested action.
This is exactly the same as before() and after() methods in kohana framework ..

Thank You in advance for your replies.

Regards,
SylveK
« Last Edit: October 16, 2010, 05:31:21 am by sylvek »

mohrt

  • Administrator
  • Sr. Member
  • *****
  • Posts: 275
    • View Profile
Re: something like preExec/postExec action
« Reply #1 on: October 16, 2010, 09:11:02 am »
there is nothing in tmvc at the moment, although it would be pretty trivial to implement. one idea, you could run your method calls through the __call() magic method, which could check for before() and after() and make use of them. But you would have to change the way you call your methods. example:

Code: [Select]
$ctl->myfunc();  // normal
$ctl->hookMyFunc() // use __call to test/exec before() after() and call myFunc() in between.

Not every elegant, but would be a work around.


mohrt

  • Administrator
  • Sr. Member
  • *****
  • Posts: 275
    • View Profile
Re: something like preExec/postExec action
« Reply #2 on: October 16, 2010, 09:33:35 am »
One more idea, you can trigger the __call() by making your methods private. Example:

Code: [Select]
class Foo {
  private function myFunc() {
    return "test\n";
  }

  private function __call($func,$parm) {
     if(is_callable($this,'before'))
       $this->before();
     $output = $this->$func();
     if(is_callable($this,'after'))
       $this->after();
     return $output;
  }
}


$foo = new Foo;

echo $foo->myFunc();

When you call myFunc() it will trigger __call() which takes care of the hooks.
« Last Edit: October 16, 2010, 09:41:34 am by mohrt »

sylvek

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: something like preExec/postExec action
« Reply #3 on: October 16, 2010, 12:03:17 pm »
One more idea, you can trigger the __call() by making your methods private. Example:
When you call myFunc() it will trigger __call() which takes care of the hooks.
Thanks for reply mohrt, but it didn't work. I get fatal error:
Code: [Select]
Fatal error: Call to private method Hello_Controller::model() from context 'tmvc' in /var/www/tinymvc/tinymvc/sysfiles/TinyMVC.php on line 108

I guess I have to write a little bit in sysfiles ;)

mohrt

  • Administrator
  • Sr. Member
  • *****
  • Posts: 275
    • View Profile
Re: something like preExec/postExec action
« Reply #4 on: October 16, 2010, 01:47:03 pm »
hmm my test here worked. Oh well, yeah the right way is to do this in the bootstrap when the controller method is called. Out of curiosity, what are you using the before/after functions for?

mohrt

  • Administrator
  • Sr. Member
  • *****
  • Posts: 275
    • View Profile
Re: something like preExec/postExec action
« Reply #5 on: October 16, 2010, 01:48:53 pm »
Thanks for reply mohrt, but it didn't work. I get fatal error:

btw be sure you have __call() in place and it does not return false. That may be the error problem.

sylvek

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: something like preExec/postExec action
« Reply #6 on: October 16, 2010, 03:57:23 pm »
Out of curiosity, what are you using the before/after functions for?
At this point I only test tmvc, try to know it better. I don't use this functions at all right now, I just think they could be usefull.

btw be sure you have __call() in place and it does not return false. That may be the error problem.
What do you mean by "__call() in place"? __call() doesn't return false, it return true;
« Last Edit: October 16, 2010, 04:00:26 pm by sylvek »

mohrt

  • Administrator
  • Sr. Member
  • *****
  • Posts: 275
    • View Profile
Re: something like preExec/postExec action
« Reply #7 on: October 16, 2010, 04:03:14 pm »
make sure __call() exists. My test executed __call() without the error.

sylvek

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: something like preExec/postExec action
« Reply #8 on: October 16, 2010, 04:23:30 pm »
make sure __call() exists. My test executed __call() without the error.

It looks like this:
Code: [Select]
class Hello_Controller extends TinyMVC_Controller {

public function before() { echo 'asdfasdf'; }

public function __call($_function, $_params) {
    if( is_callable($this, 'before') ) $this->before();

$output = $this -> $_function();

if( is_callable($this, 'after') ) $this->after();
     
return true; //$output;
}

private function model() {
// load the model
$this->load->model('Page_Model','page');

$this->view->assign('title', $this->page->get_title());
$this->view->assign('body_text', $this->page->get_body_text());

$this->view->assign('pagem', $this->page);

$this->load->library('demo','mydemo');
$this->view->assign('output', $this->mydemo->test());


var_dump( get::get('nick', 'test') );
var_dump( get::get('test', 'test') );
var_dump( get::has('test2') );

$this->load->script('myhelpers');

$this->view->display('hello_view');
}
}

request looks like this
Code: [Select]
http://127.0.0.1/projekty/tinymvc/htdocs/index.php/hello/model/nick/sylvek/test/atujest/
and I'm getting this error:
Code: [Select]
Fatal error: Call to private method Hello_Controller::model() from context 'tmvc' in /mnt/store/www/projekty/tinymvc/tinymvc/sysfiles/TinyMVC.php on line 111
I add before/after function execution in tmvc::main() and it works, but I think I shouldn't be doing anything there.
« Last Edit: October 16, 2010, 04:31:13 pm by sylvek »

mohrt

  • Administrator
  • Sr. Member
  • *****
  • Posts: 275
    • View Profile
Re: something like preExec/postExec action
« Reply #9 on: October 16, 2010, 04:37:04 pm »
Out of curiosity, what are you using the before/after functions for?
At this point I only test tmvc, try to know it better. I don't use this functions at all right now, I just think they could be usefull.

btw be sure you have __call() in place and it does not return false. That may be the error problem.
What do you mean by "__call() in place"? __call() doesn't return false, it return true;

It would be good to know what it's useful for first. If it's not particularly useful, why add it?

sylvek

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: something like preExec/postExec action
« Reply #10 on: October 16, 2010, 06:24:52 pm »
It would be good to know what it's useful for first. If it's not particularly useful, why add it?
I didn't say it has to be added, I was just wondering if such functionality is implemented. Where I work, we use the before() method to define main view. I think it's quite handy.

mohrt

  • Administrator
  • Sr. Member
  • *****
  • Posts: 275
    • View Profile
Re: something like preExec/postExec action
« Reply #11 on: October 16, 2010, 06:52:01 pm »
yea well I want to know legit uses so I can decide to implement or not :) you mean the layout view? just curious how this is used instead of letting the templates determine that?

sylvek

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: something like preExec/postExec action
« Reply #12 on: October 17, 2010, 02:18:44 am »
yea well I want to know legit uses so I can decide to implement or not :) you mean the layout view? just curious how this is used instead of letting the templates determine that?
We have few views, index (with header, footer, menu, logo etc.), preview and edit views (with different header, no menu, footer), and few more. Everyone one of them have openening and closing html, head, body tags, and somewhere in the middle $content variable.
In controller actions we open template, do what we gonna do and put it in the main view as $content.

I'm not sure I explain it good, but I believe you'll understand :)

mohrt

  • Administrator
  • Sr. Member
  • *****
  • Posts: 275
    • View Profile
Re: something like preExec/postExec action
« Reply #13 on: November 19, 2010, 08:37:12 am »
Yep that is common. I'm not sure how before/after hooks help this though. This is all handled in views.

bluudar

  • Guest
Re: something like preExec/postExec action
« Reply #14 on: November 22, 2010, 04:03:38 am »
Hi guys,

exactly about this problem I write this topic. It actually solves only the preExec problem. The constructor of the controller is actually your preExec function. In official version of tinyMVC I couldn't do load models and libraries there that is why I made this changes and it works great for me.  If someone has a better solution for this problem I am open to see it.

About the example of usage of this I could give you a simple one:
Lets imagine that we have a User Controller with methods showing pages with different user information and on every of these pages I would like to have the number of new messages received by the user. So i would load messages model get the new messages assign them to a member variable of the controller - for ex. $this->data['new_messages'] = "The new messages", and later in methods of the controller just have $this->data['other_user_info'] = "somth.." and $this->view->assign($this->data).

I think it is clearer than loading messages in every controller method.

Also it is very helpful when you want to restrict the access to the whole Controller. In constructor function you put the login check like:
Code: [Select]
if(! logged_in() ) redirect("/path/to/login");