Welcome Guest, Not a member yet? Register   Sign In
Problem with _remap function (trying to redirect) [SOLVED]
#1

[eluser]tommizzle[/eluser]
Hi all,

I have just started using CI about 2 days ago, and have come into a problem with the _remap function. I'm trying to redirect anything off the 'www.root.com/username' to 'www.root.com/profile/username', and have no idea where I'm going wrong - Any help will be appreciated.

Controller:
Code:
<?php

class Home extends Controller {
    
    function _remap($username)
    {
        
        
        $this->load->model('Home_model');
        $is_user = $this->Home_model->is_valid_user($username);
        echo $is_user;
        
        
        if($is_user == true):
            header('Location: /profile/' . $username);
            exit;
        else:
            $data = array();
        
            $data['lastjoined'] = $this->Home_model->get_last_joined(3);
            $data['representing'] = $this->Autoload_model->get_socialnetworks('random',10);
            $data['randomtip'] = $this->Autoload_model->get_random_tip();
            $data['latestnews'] = $this->Autoload_model->get_latest_news(3,50);
            
            $this->load->view('header');        
            $this->load->view('home',$data);
            $this->load->view('footer');
            
        endif;
    }

}

Model:
Code:
<?php

class Home_model extends Model {
    
    function get_last_joined($limit = '10')
    {
        $this->db->select('*');
        $this->db->from('user');
        $this->db->join('country', 'user.country_id = country.id');
        $this->db->limit($limit);
        $query = $this->db->get();
        
        return $query->result();
    }
    
    function is_valid_user($username = '')
    {
        $this->db->select('id');
        $this->db->from('user');    
        $this->db->where('user_name', $username);
        $this->db->limit(1);
        
        if($this->db->count_all_results() < 1):
            return false;
        else:
            return true;
        endif;            
    }
    
}

Apologies for the crappy code ;p
#2

[eluser]tommizzle[/eluser]
the echo $is_user; was my attempt to debug, btw
#3

[eluser]andrewtheandroid[/eluser]
hey there so you can't use the redirect() function?

in the url_helper class.

edit: like umm redirect('profile/'.$username); ?
#4

[eluser]andrewtheandroid[/eluser]
also have you tested the is_valid_user() by itself? returning the right value?

u can test if ur redirect is working by hardcoding $is_user to true and see if it works?
#5

[eluser]tommizzle[/eluser]
Hi andrew,

I just tried the redirect - still no good Sad

I have tested is_valid_user() on it's own like this:

if($is_user == false):
echo 'false';
else:
echo 'true';
endif;

it returned false on the homepage, and 404 when i tried to put a username after the www.root.com/

I think I'm definitely being insanely stupid somewhere here.

Thanks for your help,

Tom

Edit: I just tried hardcoding $is_user to true, and still got a 404 when i put a username on the end.
#6

[eluser]asylmottaket[/eluser]
Don't you need a constructor?
#7

[eluser]BrianDHall[/eluser]
www.root.com/username wouldn't work unless you were using a special route. _remap is only called if the constructor is called, so you'd need www.root.com/home to activate remap, and www.root.com/home/username to get it to redirect to profile/username.

www.root.com/username causes CI to look for the username controller unless you have routed it in some special way, which of course doesn't exist so 404.
#8

[eluser]tommizzle[/eluser]
Thanks BrianDHall.

So is there any other way to run the username off the root?

Thanks,

Tom
#9

[eluser]BrianDHall[/eluser]
[quote author="tommizzle" date="1257458339"]Thanks BrianDHall.

So is there any other way to run the username off the root?

Thanks,

Tom[/quote]

Sure, you need a sort of negative route. You define a route with regex that says "if I am not asking specifically for these controllers, then reroute it to this".

I just so happen to do exactly this. Here you go:

Code:
// Route everything but mentioned controllers to default controller, allowing short urls to work.
$route['^(?!controller|controller|controller).*'] = $route['default_controller'] . "/$0";

So stick in what you do not want rerouted, and then everything else will be rerouted to whatever you want. So long as there isn't someone with a username the same as one of your controllers (the easiest way to handle this is manually create a user with the username of each of your controllers, to reserve it and guarantee you never have a conflict), it should work as you think it would.
#10

[eluser]andrewtheandroid[/eluser]
hey tom

http://ellislab.com/codeigniter/user-gui.../urls.html

under enabling query strings you could try to enable it so you can go

root.com/index.php?username=this

but other than that I think it's prob better to make a different controller because what you are affectively doing is logging in without a password yeh?

so you could do

root.com/profile/username then in ur "profile" controller have ur code there.. use the segment() function go get ur username and if it doesn't exist redirect them to home.




Theme © iAndrew 2016 - Forum software by © MyBB