Replace url router

Hi, i'm trayng to replace part of my url routing, removing spaces ' ' by '-' but i can't.

Ex: A app called: "my app name" with my code displays:

    online/10/my%20app%20name

But want to show:

    online/10/my-app-name

In my services.php i put

    $di->set('router', function() {
        $router = new Router();

         $router->removeExtraSlashes(true);

        $router->add(
            '/online/{id}/{name}',
            array(
                'controller'    =>  'application',
                'action'        =>  'app',
                'id'            =>  1,
                'name'          =>  2,
            ))->convert('name', function($name){
                return str_replace(' ', '-', $name);
            });
        return $router
    });

According my understanding about Routing documentation ->convert should be able to do what do i need.

edited Jul '15

This can't be achieved in php without a redirect; might be achieved from htaccess, but I'm not entirely sure.

convert function is to tell the router internally how to read the url, so it will match your controller/action.



1.1k

did you also try str_replace('%20', '-', $name); ? Don't know if that would work or not...

I did it, but don't work, nothing work with this convert, i don't know why, the routes works but convert not

did you also try str_replace('%20', '-', $name); ? Don't know if that would work or not...

The router works, just not convert...

This can't be achieved in php without a redirect; might be achieved from htaccess, but I'm not entirely sure.

convert function is to tell the router internally how to read the url, so it will match your controller/action.



2.0k

@Marcos Do these URL with spaces already exist in your app? As in, do users already use this URL? Has Google indexed any of these?

@Marcos Do these URL with spaces already exist in your app? As in, do users already use this URL? Has Google indexed any of these?

My app are in development environment, i created a slug field in my table to solve this problem, it was the better way to do what i want