Welcome Guest, Not a member yet? Register   Sign In
Trouble linking to a controller function
#1

[eluser]EyeAmN8[/eluser]
I am pretty new to CI so this will probably be easy to answer. I am trying to us a link so users can logout. When I try it, I get a 404 that says page not found. I think it might have something to do with the routes.

In my views header
I have tried using site_url() with the a tag but it just tacks on my site url to the end of the existing uri, and 404's cause www.mysite.com/home/www.mysite.com/home/do_logout does not exist.
Code:
<html>
    <head>
        <title>some title</title>
    </head>
    <body>
        <a href="home/do_logout/">logout</a>

in my controller
Code:
&lt;?php
class Home extends CI_Controller{
  function _construct(){
   parent:: _construct();
   $this->check_isvalidated();
  }

  public function index($msg = NULL)
  {
   $data['title'] = "TEST site";
   $data['heading'] = "Home";
   $this->load->model('Home_model');
   $data['comments'] = $this->Home_model->get_last_ten_entries();
   $data['msg'] = $msg;
    $this->load->view('templates/header', $data);
   $this->load->view('home_view', $data);
   $this->load->view('templates/footer', $data);
  }
                // this is the function I want to use to logout
  public function do_logout()
  {
         $this->session->sess_destroy();
         redirect('login');
     }

  private function check_isvalidated()
  {
         if(! $this->session->userdata('validated')){
             redirect('../login');
         }
     }

}

in my routes
I was thinking that ['home(/:any)'] = "Home was overriding everything and not letting it through, but when I remove (/:any), I get a 404 when just trying to go home.
Code:
$route['default_controller'] = "Login";
$route['blog(/:any)'] = "Blog";
$route['home(/:any)'] = "Home";
//$route['home/do_logout'] = "Home/do_logout";
$route['404_override'] = '';

I am also using a htaccess file to remove index.php. it is in my root dir
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]


These seems like it would be a fairly simple fix of some type of configuration that I have missed, but I can't seem to find it.
Any help would be great! thanks
#2

[eluser]Aken[/eluser]
FIrst, if site_url() is giving those problems, then your base_url config setting is wrong. It should be a full URL, including http:// at the front. Otherwise just leave it blank and CI will attempt to figure it out.

Routes are processed in order from top to bottom. Once one matches, it stops looking for any other matches. Given your route example, home/:any will match and stop the checks before home/do_logout is found. Always put your most specific routes first, and then the generic catch-all ones later.
#3

[eluser]EyeAmN8[/eluser]
Your advice about properly configuring my base url fixed my issue with using site_url(), thank you very much!
I also changed the order of the routes, but I still get the 404's. I have also tried leaving a trailing " / " after the controllers function in the routes and view, but I get the same results.
Code:
$route['default_controller'] = "Login";
$route['blog(/:any)'] = "Blog";
$route['home/do_logout'] = "Home/do_logout";
$route['home(/:any)'] = "Home";

I tried playing around with enabling query strings and was successful with that.
this is in the view with query strings enabled. I added single quotes after href= only for this forum post, but not on my site.

href='&lt;?php echo site_url("c=home&m=do_logout") ?&gt;' >logout



in the view without query strings enabled


href='&lt;?php echo site_url("home/do_logout") ?&gt;' >logout

I couldn't put the href's in a code tag due to heavy spam traffic on the site. SorrySad
#4

[eluser]EyeAmN8[/eluser]
I got it! it was my routes. I took out the (/:any) from home and it worked. weird though, i didn't think if it was read from top down it would matter.
#5

[eluser]CroNiX[/eluser]
It's (:any), not (/:any)
#6

[eluser]EyeAmN8[/eluser]
got it! thanks




Theme © iAndrew 2016 - Forum software by © MyBB