Welcome Guest, Not a member yet? Register   Sign In
Controller Names with Dashes or Underscores?
#1

[eluser]seanloving[/eluser]
Hi guys,
I couldn't find the answer in the docs or forums.

When I name my controller "Who-we-are" it does not work (filename controllers/who-we-are.php). I get a 404 page not found. But "Who_we_are" seems to work fine (filename controllers/who_we_are.php).

This works
Code:
class Who_we_are extends Controller {

    function Who_we_are()
    {
        parent::Controller();

        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('array');
        $this->load->database();
    }

    function index()
    {
But this does NOT work
Code:
class Who-we-are extends Controller {

    function Who-we-are()
    {
        parent::Controller();

        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('array');
        $this->load->database();
    }

    function index()
    {


What don't I know?

Apparently the view likes either dashes or underscores:

views/who-we-are.php or views/who_we_are.php both work just fine

Thanks
--Sean Loving
#2

[eluser]TheFuzzy0ne[/eluser]
You cannot use slashes in any PHP class names, or method/function names, as it's treated as a minus symbol and results in a parse error. Also, class names in CodeIgniter should only consist of numbers, letters and underscores. The first character must be a letter in uppercase, the rest must be lowercase. You should not use camel case, and you should substitute a space with an underscore.

An example of a valid controller name - Some_controller

Views are different, as they are not class/function/method names - just file names - although I recommend you stick with view names as all lowercase, separated with underscores.
#3

[eluser]seanloving[/eluser]
Thanks for the fast reply. In this case I think maybe your URL Helper function url_title() should use 'underscore' as default (instead of the '-' as default). Cheers.
#4

[eluser]Dam1an[/eluser]
If you still feel the need to have a dash in the controller (or more specifically the URL) you can always use routes
#5

[eluser]TheFuzzy0ne[/eluser]
Hi, Sean.

url_title() is not for controller/method names - it's for converting URL segments into strings that are compatible with CodeIgniter's default allowed URI characters. Please see the [url="http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html"]user guide[/url] for further information.

Your controller and method names should already be URL-safe. Please see the [url="http://ellislab.com/codeigniter/user-guide/general/controllers.html"]user guide[/url] for further information on that, and also the [url="http://ellislab.com/codeigniter/user-guide/general/styleguide.html#class_and_method_naming"]PHP style guide[/url].
#6

[eluser]Dam1an[/eluser]
[quote author="TheFuzzy0ne" date="1242511000"]Hi, Sean.

url_title() is not for controller/method names - it's for converting URL segments into strings that are compatible with CodeIgniter's default allowed URI characters. Please see the [url="http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html"]user guide[/url] for further information.

Your controller and method names should already be URL-safe. Please see the [url="http://ellislab.com/codeigniter/user-guide/general/controllers.html"]user guide[/url] for further information on that, and also the [url="http://ellislab.com/codeigniter/user-guide/general/styleguide.html#class_and_method_naming"]PHP style guide[/url].[/quote]

Wow you managed to get 3 references to the user guide in one post, thats a new record for you Tongue
#7

[eluser]TheFuzzy0ne[/eluser]
Tell me about it. Now I'm all URL'd out. Sad
#8

[eluser]TheFuzzy0ne[/eluser]
[quote author="seanloving" date="1242508389"]Thanks for the fast reply. In this case I think maybe your URL Helper function url_title() should use 'underscore' as default (instead of the '-' as default). Cheers.[/quote]

Oh, and one more thing. If you would prefer underscores, you can extend/override the helper[url="http://ellislab.com/codeigniter/user-guide/general/helpers.html"].[/url] (URL hidden so Dam1an can't find it).

EDIT: I sit corrected. You can call on url_title() like this:

Code:
$segment = url_title($segment, '_');
#9

[eluser]moodh[/eluser]
TheFuzzyOne, I doubt the issue is about the controller names but rather the presentation in the URL, as dashes is more SEO friendly than underscores is.
Sure, routes is the "correct" way to solve this, but it's still a good question as it'd be damn neat if it actually worked naming classes (hence urls) directly with dashes.

But ye, routes is the way to go.
Of course, I might be mistaken, but this is my view on the thread =)
#10

[eluser]Myles Wakeham[/eluser]
[quote author="narcisha" date="1242513125"]TheFuzzyOne, I doubt the issue is about the controller names but rather the presentation in the URL, as dashes is more SEO friendly than underscores is.[/quote]

I'm not absolutely sure that is the case. SEO rules change frequently and its my understanding from reading the Google PageRank patent application that dashes and underscores are considered equal value in SEO. Of course this could be a completely false statement tomorrow....

Myles




Theme © iAndrew 2016 - Forum software by © MyBB