Welcome Guest, Not a member yet? Register   Sign In
urls with dashes, instead of underscores
#11

[eluser]Jayson Ng[/eluser]
Hi "LAMP Coder"

do you mind sharing the code(s) on how you solved this?

like what regex in the config file did you use?

what about the _remap?

thanks,
#12

[eluser]LAMP Coder[/eluser]
[quote author="jaysonng" date="1247779630"]Hi "LAMP Coder"

do you mind sharing the code(s) on how you solved this?

like what regex in the config file did you use?

what about the _remap?

thanks,[/quote]


I used following code:


I'm sure this can be done in a better way, but this is what I used:
Add following to your routes.php file
Code:
$route['([a-z]+)-([a-z]+)-([a-z]+)(.*)'] = "$1_$2_$3$4";
$route['([a-z]+)-([a-z]+)(.*)'] = "$1_$2$3";


in your controller, all following method:
Code:
function _remap($method)
    {
            $method = str_replace('-','_',$method);
            if(method_exists($this, $method))
            {
                $this->$method();
            }
            else
                show_404();
        
    }

while defining your methods, use underscores instead of dashes.
#13

[eluser]Jayson Ng[/eluser]
@lamp coder

perfect! it works beautifully!

thanks!
#14

[eluser]Jayson Ng[/eluser]
had problems with arguments not getting passed.

and since I wanted this on all my controllers, I put _remap into my MY_Controller.

Code:
// Remap dashes "-" in the URL to underscores "_"
    function _remap($method = NULL)
    {
        $size            = $this->uri->total_segments();
        $method_key        = array_search($method, $this->uri->segments);

        $method = str_replace('-','_',$method);
        if(method_exists($this, $method))
        {
            // is there a better way to pass methods? dynamically?
            // this only works with 5 arguments.
            $this->$method(
                      $this->uri->segment($method_key + 1)
                ,     $this->uri->segment($method_key + 2)
                ,     $this->uri->segment($method_key + 3)
                ,     $this->uri->segment($method_key + 4)
                ,     $this->uri->segment($method_key + 5)
                );
        }
        else
        {
            show_404();
        }
    }

and to solve the problem the arguments not passing, I did this.
crude but it works. I'm hoping for a more elegant solution if anybody has ideas.

This is a general function that will work (so far) for all controllers.
#15

[eluser]LAMP Coder[/eluser]
You can always use following:

Code:
call_user_func(array($this,$method),$arguments_array);
#16

[eluser]LAMP Coder[/eluser]
instead of:
Code:
$this->$method(
                      $this->uri->segment($method_key + 1)
                ,     $this->uri->segment($method_key + 2)
                ,     $this->uri->segment($method_key + 3)
                ,     $this->uri->segment($method_key + 4)
                ,     $this->uri->segment($method_key + 5)
                );

You'll have to convert the arguments into an array first though.
#17

[eluser]Jayson Ng[/eluser]
@LAMP Coder

That's awesome. I didn't know about call_user_func.

thanks for all the help!
#18

[eluser]Jayson Ng[/eluser]
fyi, I had to use

Code:
call_user_func_array(array($this,$method),$args);

to get it to work correctly. Smile




Theme © iAndrew 2016 - Forum software by © MyBB