0

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');
    }
}
?>
7
  • 1
    The answer you're looking for involves routing. Which framework is this? Check their documentation. Commented Aug 24, 2012 at 17:38
  • As far as I know , the default url structre is like: controller/action/otherParameters. This is why you need that index/ part , it's the action of viewing the profile. Commented Aug 24, 2012 at 17:38
  • @OfirBaruch that's correct, but a routing script will do what OP wants. index/ is not always necessary. Commented Aug 24, 2012 at 17:39
  • @BartTarantino Wow, that was a brain fart. I didn't see that. Commented Aug 24, 2012 at 17:39
  • codeigniter.com/user_guide/general/routing.html Commented Aug 24, 2012 at 17:40

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.