AltoRouter icon indicating copy to clipboard operation
AltoRouter copied to clipboard

Default value Param

Open nikmauro opened this issue 10 years ago • 8 comments

Hello it's possible to set in rule default value? [lang:language='en']

$router = new AltoRouter();
$router->setBasePath();
$router->addMatchTypes(array('lang' => '[A-Za-z]{2}'));

$router->map('GET', '/', '', 'home');
$router->map('GET', '/[lang:language]/rss', '', 'rss');

echo $router->generate('rss');
/*
output
[lang:language]/rss
*/

nikmauro avatar May 07 '14 09:05 nikmauro

Yes, for example :

class myRouter extends AltoRouter {

    public static $default_routes = array('controller' => array(
        'method' => "GET|POST",
        'route' => "/[:module]?/[:controller]?/[:action]?",
        'target' => "module#controller#action"
    ));

    function __construct($routes = array(), $basePath = '', $matchTypes = array()) {
            $R = array();
            foreach(static::$default_routes as $name => $route)
                $R[] = array($route['method'], $route['route'], $route['target'], $name);
            $this->addRoutes($R);
        parent::__construct($routes, $basePath, $matchTypes);
    }
}

flavi1 avatar May 09 '14 13:05 flavi1

@flavi1 That is not an answer to his problem. Anyway, this is currently not supported but I can see the use of it.

koenpunt avatar May 09 '14 13:05 koenpunt

Maybe It's better if AltoRouteur remains light as possible? Don't you think?

flavi1 avatar May 09 '14 13:05 flavi1

But it's light. I write a patch.

flavi1 avatar May 09 '14 13:05 flavi1

This may be helpful for generate function. If not set a parameter is better to show a default value instead a parameter name.

example witth default value in parameter language :en $router->generate('rss'); htp:/----/en/rss.php

current example if param not set $router->generate('rss'); htp:/----/[language]/rss.php

nikmauro avatar May 09 '14 13:05 nikmauro

@koenpunt : Hum, ok sorry you wrote a patch ;)

flavi1 avatar May 09 '14 13:05 flavi1

@nikmauro : I'm not sure to understand. Maybe just set a language matchType in the constructor when you create your object?

$routes = array();
$basePath = '';
$matchTypes = array('lang' => '[A-Za-z]{2}');
$router = new altoRouter($routes, $basePath, $matchTypes);

After you just have to do :


$prefix = "'/[lang:language]/";
$maps = array('rss', 'home');
foreach($maps as $m)
    $router->map('GET', '/[lang:language]/'.$m, '', $m);

This is what you mean?

flavi1 avatar May 09 '14 14:05 flavi1

Oh no! Ok I understand!!!

flavi1 avatar May 09 '14 14:05 flavi1