views:

32

answers:

2

Here is a function in a Controller that lets a user log out. But how do I enable the user to do this? I would think the answer is to put a hyperlink in a View. But I can't figure out how to make a hyperlink that invokes the function. (And I'd rather not build a form ...) Source for this code

class Auth extends Controller {
   ....
  function logout() 
  {
    $this->data['title'] = "Logout";

    //Library function
    $logout = $this->ion_auth->logout();

    //redirect them back to the page they came from
    redirect('auth', 'refresh');
  }
   ....
}
+1  A: 

the relevant docs are here: http://codeigniter.com/user_guide/general/routing.html

essentially, you want to create the link to http://site.com/Auth/logout. This will invoke the function on your controller.

aaronasterling
Thanks, this is 1 of 2 valuable-looking answers. Attempt results in 404 'Page not found'. I am going to dismantle my .htaccess directives, they seem to make everything difficult.
Smandoli
+1  A: 

Just write a hyperlink in your view.

echo anchor('auth/logout', 'Logout');

URI routing in CodeIgniter by default follows the convention that the first URI segment corresponds to the controller name and the second to the method name. If someone follows the link then the logout method of the Auth controller class will be invoked.

The anchor function to generate the hyperlink is part of the url helper functions. Remember to include the url helper through autoloading or explicity in your code :

$this->load->helper('url');
Stephen Curran
Thanks, this is 1 of 2 valuable-looking answers. Attempt results in 404 'Page not found'. I am going to dismantle my .htaccess directives, they seem to make everything difficult.
Smandoli
Probably the attempt to redirect around index.php kept me from getting this. Thanks.
Smandoli
Do you have a refresh method in the Auth class? Perhaps its the redirect after logging out that is causing the 404...
Stephen Curran
(Offhand) I doubt I do ... I will keep it in mind when I reinstate the .htaccess directives.
Smandoli