Welcome Guest, Not a member yet? Register   Sign In
Stop controller execution from controller's private method
#1

[eluser]Nameless One[/eluser]
Is it possible to somehow stop the execution of controller but continue with execution of CI from a controller's private method which was called from controller "action"?

To my knowledge, the only way to stop controller execution without totally stopping CI is to do a return from the "action" method. If I do the return from private method, it will only return to the "action" method which will continue the controller execution. exit() or die() would completely stop CI. Right now, I'm stuck with:

Code:
if ($this->_private_method())
    return;

bit I would like to make it a one-liner since I will have to call _private_method() in almost every controller "action".

Note: I use "action" to refer to a controller's public function which is called in response to user's request. This is not official CI terminology as far as I'm aware but many other frameworks use it.
#2

[eluser]vitoco[/eluser]
the only way that comes in to my mind it's that $this->_private_method() returns true/false , and put the call in the public method in the same conditional that you post.

to do this "one-liner", use command "sed" in the terminal

sed -i 's/[original_string]/[new_string]/g' *.php

Saludos
#3

[eluser]Nameless One[/eluser]
[quote author="vitoco" date="1289590157"]the only way that comes in to my mind it's that $this->_private_method() returns true/false , and put the call in the public method in the same conditional that you post.

to do this "one-liner", use command "sed" in the terminal

sed -i 's/[original_string]/[new_string]/g' *.php

Saludos[/quote]

Oh, a misunderstanding. My _private_method() already returns TRUE or FALSE. Why else would I put it in an if in my example?

About one-liner, I know I can write my example code in a single line but what I really want is to have only a function call, without an if.
#4

[eluser]vitoco[/eluser]
ok...so checking this chart
http://ellislab.com/codeigniter/user-gui...pflow.html
you may call _display method to render the view and after that make an exit();...so the controller stops. Not elegant, but it may work

Saludos
#5

[eluser]Nameless One[/eluser]
No can do. Calling exit() at any point will completely stop the execution of code for that request. What I want is to quit the controller but still have CI render the output. According to the chart, I want to skip the rest of the Application Controller phase and continue with View phase.
#6

[eluser]vitoco[/eluser]
[quote author="Nameless One" date="1289592701"]...still have CI render the output..[/quote]

that's why you must call _display() method , to render the output
#7

[eluser]Nameless One[/eluser]
Oh, sorry, I though you were referring to the display_override hook. Since I already use display_override hook to do some automatic output formatting, I'll have to check if calling _display() method will also called the hooked function. If it does work that way, it might just be what I need. Thanks for the idea.
#8

[eluser]Nameless One[/eluser]
I've finally found some time to try this. _display() on its own wasn't enough to solve my problem, but it lead me to a solution. Instead of trying to stop controller execution, which I now think is impossible in CI, I decided to enhance my controller class with a method that does everything I would like CI to do after I stop the controller execution. It all comes down to:

Code:
function end_controller_execution()
{
    $this->hooks->_call_hook('display_override');
    $this->output->_display();
    exit()
}

That's it. Works like a charm, and I no longer have to put my error detection functions in ifs. If I wanted to use the post system hook I'd have to call it as well.
#9

[eluser]vitoco[/eluser]
you are welcome !!! jajajjajaja




Theme © iAndrew 2016 - Forum software by © MyBB