Welcome Guest, Not a member yet? Register   Sign In
Replace underscore (_) with dashes (-) in URL
#1

[eluser]livewirerules[/eluser]
i have been going through the website and googling for a working solution to replace all the _ with - for the urls. Below is a solution which i found, but doesn't work.

Code:
<?php
class MY_Router extends CI_Router
{
    function _set_request($segments = array()) {
        parent::_set_request(str_replace('-', '_', $segments));
    }
}
?>

i added to the core folder but nothing happens to the url. Can someone suggest be a good solution where i can replace the underscores. i don't want to use the routing feature because i have over 300 urls.

Any help will be appreciated.
#2

[eluser]livewirerules[/eluser]
below is another solution which doesn't work..

Code:
Now create a “pre-system” hook by adding these lines to your ‘config/hooks.php’ file:

$hook['pre_system'] = array(
    'class'    => '',
    'function' => 'prettyurls',
    'filename' => 'myhooks.php',
    'filepath' => 'hooks',
    'params'   => array()
);

Finally ‘myhooks.php’ file within the ‘application/hooks’ folder and add the following function

function prettyurls() {
    if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '') {
        $newkey = str_replace('-','_',key($_GET));
        $_GET[$newkey] = $_GET[key($_GET)];
        unset($_GET[key($_GET)]);
    }
    if (isset($_SERVER['PATH_INFO'])) $_SERVER['PATH_INFO'] = str_replace('-','_',$_SERVER['PATH_INFO']);
    if (isset($_SERVER['QUERY_STRING'])) $_SERVER['QUERY_STRING'] = str_replace('-','_',$_SERVER['QUERY_STRING']);
    if (isset($_SERVER['ORIG_PATH_INFO'])) $_SERVER['ORIG_PATH_INFO'] = str_replace('-','_',$_SERVER['ORIG_PATH_INFO']);
    if (isset($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = str_replace('-','_',$_SERVER['REQUEST_URI']);
}
#3

[eluser]TheFuzzy0ne[/eluser]
Please could you explain why you want to do that? Are you planning on changing the links on all of your pages so that they use hyphens instead of underscores? If anyone is linking to your site, that may break their links.

You're going to run into trouble if you do this, because you can't use hyphens in class/method names.

Just out of interest, what prompted the change of mind?
#4

[eluser]Aken[/eluser]
[quote author="TheFuzzy0ne" date="1364669682"]You're going to run into trouble if you do this, because you can't use hyphens in class/method names.[/quote]

That's exactly what he's trying to do -- automatically turn hyphens in his URL into underscores, so that they will map to controllers/methods without needing routes.

CI 3.0 added this feature. I suggest either:

A) Update your application to CI 3.0. You can find the in-progress dev version here.

B) Extend the Router class and replace the _validate_request() method from here.

Note that there may be other changes in this method that I forgot about that may affect your app. I don't remember everything off hand, but that's where the str_replace stuff happens. So might get you in the right direction for now.
#5

[eluser]livewirerules[/eluser]
[quote author="TheFuzzy0ne" date="1364669682"]Please could you explain why you want to do that? Are you planning on changing the links on all of your pages so that they use hyphens instead of underscores? If anyone is linking to your site, that may break their links.

You're going to run into trouble if you do this, because you can't use hyphens in class/method names.

Just out of interest, what prompted the change of mind?[/quote]

The main reason is that (-) are better as in SEO perspective. Because dashes are treated as word separators and underscores are treated as word joiners.

My site is brand new, still on testing phase. the urls with _ looks very odd.

Those are the main reasons that i need to change the urls...
#6

[eluser]livewirerules[/eluser]
[quote author="Aken" date="1364686919"][quote author="TheFuzzy0ne" date="1364669682"]You're going to run into trouble if you do this, because you can't use hyphens in class/method names.[/quote]

That's exactly what he's trying to do -- automatically turn hyphens in his URL into underscores, so that they will map to controllers/methods without needing routes.

CI 3.0 added this feature. I suggest either:

A) Update your application to CI 3.0. You can find the in-progress dev version here.

B) Extend the Router class and replace the _validate_request() method from here.

Note that there may be other changes in this method that I forgot about that may affect your app. I don't remember everything off hand, but that's where the str_replace stuff happens. So might get you in the right direction for now.[/quote]

will it be an issue if i update to version 3? because the application is based on CI2..
#7

[eluser]TheFuzzy0ne[/eluser]
There are probably some fundamental difference between CodeIgniter 2 and 3. Just rename your existing ./system directory, and drop the system directory from CI 3 in it's place, and test it out. You may come across some problems that are simple to solve, then again, it might be a nightmare. CI 3 does it's best to stay compatible, but you never know. If it doesn't work out, you can just delete the ./system directory, and rename the old one back to system.

However, if this is the only feature you're wanting, then just extending your existing core will probably be the best way to go.
#8

[eluser]livewirerules[/eluser]
Thanks for the details. But what do i need to do to achieve that result extending the core? As in my first post that solution was supposed to work, but it doesn't work for me. Is anything wrong with that code?
#9

[eluser]TheFuzzy0ne[/eluser]
I don't understand why either of the snippets you posted aren't working, other than the files the code was in weren't correctly named. Did you check to see if they were being called?

What if you wanted an underscore in the URL? I don't think the solution is to replace ALL hyphens with underscores. I think you should only do it where it's absolutely necessary, and that is for the directory, class and method properties. I'd go with something like this (untested):

./application/core/Router.php
Code:
class MY_Router extends CI_Router
{
    /**
    * Set the class name
    *
    * @access public
    * @param string
    * @return void
    */
    function set_class($class)
    {
        $this->class = str_replace(array('/', '.', '-'), array('', '', '_'), $class);
    }

    /**
    * Set the directory name
    *
    * @access public
    * @param string
    * @return void
    */
    function set_directory($dir)
    {
        $this->directory = str_replace(array('/', '.', '-'), array('', '', '_'), $dir).'/';
    }

    /**
    * Set the method name
    *
    * @access public
    * @param string
    * @return void
    */
    function set_method($method)
    {
        $this->method = str_replace('-', '_', $method);
    }
}

Now you can use underscores (if the need arises) in your URLs as well as hyphens.

Hope this helps. The actual GET array would remain unmodified, which may or may not be what you want. It should mean that the URI library will work exactly as expected.
#10

[eluser]livewirerules[/eluser]
its very odd... ./application/core/Router.php when i use that name (without MY_) i get this error
Code:
Fatal error: Class 'CI_Router' not found in C:\wamp\www\test\application\core\Router.php

when i use the file name as MY_Router.php i dont get any error but nothing happens to the URLs. is there anything else that i need to do after adding the code in the core folder?




Theme © iAndrew 2016 - Forum software by © MyBB