1

I'm having some problems learning how to program proper URLs in codeigniter ... im finding that controller names cant have hyphens, only underscores.

Ill tell you the process in doing, and if someone could let me know where im going wrong, that would be great.

Ok, im my view I have a button that links to a page, for example about_us.

<a href="<?php echo base_url().'about_us'; ?>">About Us</a>

This then loads the about_us.php controller, inside the controller i have

$this->load->view('about_us');

That then loads about_us.php in the view and shows the page.

I have found that it doesnt have a problem with underscores, but I want to use hyphens for URLs instead of underscores.

When I try and call a controller about-us.php it doesnt work, when I rename the class header class About-us extends Controller { it doesnt work :S

I'm really confused how to get good looking URLs.

Any advice would be grand.

Cheers

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
Cecil
  • 1,609
  • 5
  • 21
  • 26

1 Answers1

1

A PHP class cannot have a hyphen in its name. That's why naming your class About-us didn't work.

If you're set on having hyphens in your URLs, you should look at CodeIgniter's custom URI routing.

http://ellislab.com/codeigniter/user_guide/general/routing.html

See the section on regular expressions down near the bottom of the page. You can set up a regular expression to replace the hyphens in your URLs to underscores - that way, your users will see the hyphens, but CodeIgniter will use the underscores.

xiidea
  • 3,344
  • 20
  • 24
Eric G
  • 4,018
  • 4
  • 20
  • 23
  • Thank for that ... What about the url_title() function ... Does this do the same thing? ... im quite unsure on this – Cecil Mar 05 '11 at 10:36
  • url_title() is just for generating links, it doesn't change anything about the actual URLs in use. – Eric G Mar 05 '11 at 10:53
  • 1
    I found this as well, it gives details about implementing a solution similar to what you're looking for: http://stackoverflow.com/questions/2428134/codeigniter-routes-regex/2432405#2432405 – Eric G Mar 05 '11 at 10:53