1
votes

I have a controller called user which just loads the user profile page for now

class user extends CI_Controller {

    public function __construct(){
        parent::__construct();
    }

    public function index($username = null){
        //load index page
        $this->load->view('profile/index');
    }

}

i have also routed it so i can load it from user/$username in routes

//user profiles pretty url
$route['user/(:any)'] = "user/index/$1";

the thing is i would like to change it and allow directly the users to go to their profiles without typing user/$username and instead $usernamd like mysite.com/$username...

I tried it but it messes up everything.how can i achieve this?

Thanks.

3

3 Answers

1
votes

I guess the only way to achieve something like this is to add all other controllers to your routes file.

You could try something like this

$route['controller'] = "controller";
$route['controller/(:any)'] = "controller/$1";
$route['(:any)'] = "user/$1";

Combined with the _remap function as stated here. In your users controller.

0
votes

Have you heard of the _remap function? If you replace the index() function with this:

public function _remap($username = null) {
    $this->load->view('profile/index');
}

It will probably work. You don't have to use the routes.php.

0
votes

I used something like this for my users ; this "p" function in my users controller, mysite.com/users/p/$user_id , routes are good but I solved it like this, you could also do it do index function if you don't want something like "p"

function p() 
{
$total_slashes = count ( $this->uri->segment_array () );
$last = end ( $this->uri->segments );
if ($total_slashes == 3) {
$data ['userdetails'] = $this->users_model->userDetails ( $last );
// $last is our user_id
$this->load->view('profile/index');
}
}