views:

376

answers:

2

Hello,

I'm hoping that this will be a simple question that someone can answer. I'm looking to build a CodeIgniter application that I can build pretty easily in regular PHP.

Example: I would like to go to http://locahost/gregavola and have rewritten using htaccess file to profile.php?user=gregavola. How can I do this in CodeIgniter?

Usually in htaccess I could write ^(\w+)$ profile.php?user=$1 but that won't work with the paths in CodeIgniter.

Any suggestions?

+1  A: 

CodeIgniter turns off GET parameters by default; instead of rewriting the URL to a traditional GET style (IE, with the ?), you should create a user controller and send the request to:

http://localhost/user/info/gregavola

Then in the user controller, add the following stub:

function info($name)
{
    echo $name;
}

From here you would probably want to create a view and pass $name into it:

  $data['name'] = 'Your title';
  $this->load->view('user_info', $data);

You can find all of this in the CodeIgniter User Guide, which is an excellent resource for getting started.

Justin Ethier
I was just giving an example of what I did in PHP - not sure how it work in CodeIgniter.How you can create a view of variable? Basically - http://localhost/gregavola - how can that get redirect to a static page so I figure out who the user is?I apologize for my lack of experience with CodeIgniter - but I'm just trying to learn.
Is there a way to not include the users/ or info/? So its `localhost/gregavola`
A: 

To map localhost/gregavola to a given controller and function, modify the routes file at application/config/routes.php like so:

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

Routes are run in the order they are received, so if you have other routes like localhost/application/dosomething/, you will want to include those routes first so that every page in your entire app doesn't become a user page.

Read more about CI routes here: http://codeigniter.com/user_guide/general/routing.html

Good luck!

Summer