我只能改變路線/files[/action[/:fileId]]
設立的路線,糾正我,如果我錯了
你沒看錯,這將是一個有效的途徑。
還有什麼我需要改變的路線定義?
如果添加了fileId
作爲一個可選路線PARAM那麼你需要做downloadAction()
內的一些人工檢查,以確保它被設置。
另一種解決方案是將路線分隔成兒童,這樣可以確保除非您在每條路線上都有正確的參數,否則不會匹配。
'files' => array(
'type' => 'Segment',
'options' => array(
'route' => '/files',
'defaults' => array(
'controller' => 'Application\Controller\Files',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'download' => array(
'type' => 'Segment',
'options' => array(
'route' => '/download/:fileId',
'defaults' => array(
'action' => 'download',
),
'constraints' => array(
'fileId' => '[a-zA-Z0-9]+',
),
),
),
'upload' => array(
'type' => 'Literal',
'options' => array(
'route' => '/upload',
'defaults' => array(
'action' => 'upload',
),
),
),
),
),
我怎麼訪問fileId
的downloadAction
裏面最簡單的方法是將use the Zend\Mvc\Controller\Plugin\Params
controller plugin來去路線參數)。
// FilesController::downloadAction()
$fileId = $this->params('fileId');
或者專門從航線
// FilesController::downloadAction()
$fileId = $this->params()->fromRoute('fileId');
感謝一個非常清晰和完整的答案。 – Bogdan 2014-09-04 13:18:03