Welcome Guest, Not a member yet? Register   Sign In
Abort execution and call _output() method
#1

[eluser]Hank the Punk[/eluser]
Hello.

In the controller constructor i'm calling a function in the user model like this schema:
Code:
class Some_class extends CI_Controller
{
public function __construct()
{
  parent::__construct();
  // Stuff to load user model
  $this->user->check_permission();
}

public function index()
{
  // Do whatever...
}
}

Code:
function check_permission()
{  
if (! $this->is_registered())
{
  redirect('members/login', 'refresh');
  die('Youre not logged'); // Case of error
}

if (! $this->has_access_to_controller())
{
  redirect('error403', 'refresh');
  die('Access denied');
}
}

Instead of using redirect methods, can I load some views and then bypass the execution of the current controller (for instance the index() method) and execute the _output method ?? Something like this:

Code:
function check_permission()
{  
if (! $this->is_registered())
{
  $this->load->view('login');
  call_controller_output() // echoes whatever
  exit(); // Bypass index() method in controller
}

if (! $this->has_access_to_controller())
{
  $this->load->view('denied');
  call_controller_output() // echoes whatever
  exit(); // Bypass index() method in controller
}
}

Thanks !!!
#2

[eluser]zauber[/eluser]
I needed something similar for some authentication code I'm working on.

I ended up going with setting the exception handler to a function of my own, then throwing an custom exception. In the exception handler, you still have access to the CI object and all it can do, but you're not executing your controller anymore.

Since exceptions take you out of the main CI flow, you don't get the part where it pushes the output buffer at the end. You could
probably call that yourself though, or just load a view with true and echo it.

So something like this, perhaps:

Code:
function index() {
        //yada yada... decide to fail:
       $this->_exit();
    }

    function _exit() {
        set_exception_handler(array($this, '_exit_handler'));
        throw new Exception(); //at this point, we completely jump out of executing our controller, and can't come back.
    }

    function _exit_handler() {
        restore_exception_handler();
        echo $this->load->view('my_view', array(), TRUE);
    }
#3

[eluser]Hank the Punk[/eluser]
Thanks a lot !!!
Just what I was looking for. What a strange way to wipe all output data and abort execution !! jaja I think future versions should have this functionality.

As I was aborting execution outside the controller, I had to add a parameter to pass the current controller to my function:
Code:
public function function check_permission($controller)
{
  // code code code... crash!
  set_exception_handler(array($controller, '_exit_handler'));
  throw new Exception('bye bye');
}
#4

[eluser]zauber[/eluser]
Glad it helped!

Not sure if you're asking me how to deal with the $controller parameter, but If you need it in the "_exit_handler" according to my example, you can just set it
on the exception and use it in the exception handler later.

So...

Code:
function _exit($controller) {
  $exit = new Exception(); //maybe should use custom class here, but whatever...
  $exit->controller = $controller;
  set_exception_handler(array($this, '_exit_handler'));
  throw $exit;
}

function _exit_handler($exit) {
  $controller = $exit->controller;
  //do something with controller...
}
#5

[eluser]Hank the Punk[/eluser]
Woops!! It's fine Wink just fixed a mistake in the code in #2
Code:
set_exception_handler(array($controller, '_exit_handler'));
instead of
Code:
set_exception_handler(array($this, '_exit_handler'));

Transcription fail jeje
Thanks again !!




Theme © iAndrew 2016 - Forum software by © MyBB