Bart De Kimpe
Bart De Kimpe

Reputation: 359

Passing variable to controller (not a function)

I like to pass a variable to a controller trough the URL like this:

a href="<?php print base_url('/profile/' . $this->session->userdata('id')); ?>">Profiel</a>

/profile/ = controller
$this->session->userdata('id') = variable

Now I want to give it to Profile but it only works when I do /profile/index/1 and I like to it like this: /profile/1

When actually to use a new controller?

This is my controller:

<?php

class Profile extends CI_Controller 
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('login');
        $this->load->helper('url');
    }

    function index()
    {
        $this->load->view('profile_view');
    }
}
?>

Upvotes: 0

Views: 78

Answers (2)

vfsoraki
vfsoraki

Reputation: 2307

Or you could use _remap function in your controller class. Read the related part in users guide. It is under "controllers" part.

Upvotes: 0

Bart De Kimpe
Bart De Kimpe

Reputation: 359

Found it,

I just need to add this line to the routes.php file in the config folder:

$route['profile/(:num)'] = "profile/index/$1"

Then when profile/1 is entered it will automatically think it's profile/index/1

Upvotes: 1

Related Questions