AltoRouter
AltoRouter copied to clipboard
Default value Param
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
*/
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 That is not an answer to his problem. Anyway, this is currently not supported but I can see the use of it.
Maybe It's better if AltoRouteur remains light as possible? Don't you think?
But it's light. I write a patch.
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
@koenpunt : Hum, ok sorry you wrote a patch ;)
@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?
Oh no! Ok I understand!!!