选项一:从你的 indexAction
$id = $routeMatch->getParam('id', false);
if (!$id)
$id = 1; // id was not supplied set default one note this can be added as constant or from db ....
选项二:在 module.config.php 中设置路由
'product-view' => array(
'type' => 'Literal',
'options' => array(
'route' => '/product/view',
'defaults' => array(
'controller' => 'product-view-controller',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:cat][/]',
'constraints' => array(
'cat' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
在你的控制器中:
public function indexAction()
{
// get category param
$categoryParam = $this->params()->fromRoute('cat');
// if !cat then get random category
$categoryParam = ($categoryParam) ? $categoryParam : $this->categories[array_rand($this->categories)];
$shortList = $this->listingsTable->getListingsByCategory($categoryParam);
return new ViewModel(array(
'shortList' => $shortList,
'categoryParam' => $categoryParam
));
}