CodeIgniter Forums
A view() to die() for - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: A view() to die() for (/showthread.php?tid=33564)



A view() to die() for - El Forum - 08-31-2010

[eluser]slowgary[/eluser]
Hi all,

I'm tinkering with a basic auth system (I know there are some good ones out there already, but this is just for fun).

My approach is very basic, but for some reason it's not working. Here's my pseudocode:
Code:
class SecureController extends Controller
{
     function SecureController()
     {
          parent::Controller();

          // is_logged_in() returns true/false depending
          if(! $this->is_logged_in()) {
               $this->load->view('login');
               die();
          }
     }
}

class Admin extends SecureController
{
     function index()
     {
          echo 'admin controller';
     }
}

The problem is that the 'login' view is never shown. I know output is buffered in CodeIgniter, so I'm guessing that die() stops the output from being sent to the browser. If I omit die(), however, then 'admin controller' is displayed followed by the contents of the login view.

Is it possible to stop code execution after displaying a view?

Thanks for reading.


A view() to die() for - El Forum - 08-31-2010

[eluser]danmontgomery[/eluser]
The problem with doing it in the controller constructor is that you can't easily stop the controller from executing after the constructor is called (after the object is insantiated)... You probably want to save the current uri, then redirect... Then, once login is successful, redirect again to the saved uri. Something like:

Code:
$this->session->set_flashdata('redir', $this->uri->uri_string());
redirect('login');



A view() to die() for - El Forum - 08-31-2010

[eluser]slowgary[/eluser]
I thought about using a redirect, but would rather handle it the first time around. Oh well. Redirect it is. Thanks for your help!


A view() to die() for - El Forum - 09-01-2010

[eluser]majidmx[/eluser]
Hi,

Instead of :
Code:
$this->load->view('login');
die();

do a return :

Code:
$this->load->view('login');
return('');

It prevents the execution of the rest of the controller, while the main CI object is still live and continues loading the view.


A view() to die() for - El Forum - 09-01-2010

[eluser]danmontgomery[/eluser]
[quote author="majidmx" date="1283375208"]It prevents the execution of the rest of the controller, while the main CI object is still live and continues loading the view.[/quote]

This isn't true...

It prevents the execution of the rest of the method (in this case the constructor), it doesn't prevent the execution of other controller methods.


A view() to die() for - El Forum - 09-01-2010

[eluser]majidmx[/eluser]
Yeah well said, actually that's what I meant. It prevents the execution of the current method.
And it achieves the desired behavior.


A view() to die() for - El Forum - 09-01-2010

[eluser]slowgary[/eluser]
It unfortunately doesn't achieve the desired behavior either, since the desired behavior would be to stop the index() method from executing in the admin controller, while still outputting the view outputting the view from the constructor of its parent class.

Ultimately, I solved it by creating a Login controller and a redirect, in addition to the SecureController base class.

Something like this:
Code:
class SecureController extends Controller
{
     function SecureController()
     {
          if(strtolower(get_class($this)) != 'login' AND $this->is_logged_in() === FALSE)
          {
               $this->session->set_userdata('redirect', $this->uri->uri_string());
               redirect('/login');
          }
     }
}

class Login extends SecureController
{
     function _remap()
     {
          //if post data, check login

          //if valid credentials, $this->session->set_userdata('logged_in')

          // else send to login view
     }
}

class Admin extends SecureController
{
     function index()
     {
          //this function will only run if the user is logged in.
     }
}