爲了有一個ZF2模塊的通用/標準路由系統,這是我只是一個Controller 「模塊\控制器\指數」 的解決方案(默認控制器):
'router' => array(
'routes' => array(
'default' => array(
'type' => 'Literal',
'options' => array(
'route' => '/', // <======== this is take the first step to our module "profil"
'defaults' => array(
'module' => 'profil',
'controller' => 'profil\Controller\Index',
'action' => 'index',
),
),
),
'profil' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[profil][/:action]', // <======== this is take the next steps of the module "profil"
'constraints' => array(
'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(// force the default one
'module' => 'profil',
'controller' => 'profil\Controller\Index',
'action' => 'index',
),
),
),
),
),
然後在我們的控制器「PROFIL \控制器\目錄」,我們有三項行動‘索引’‘家’‘signout’:
public function indexAction()
{
if ($this->identity()) {
return $this->redirect()->toRoute('profil',array('action'=>'home'));
} else {
// ......
$authResult = $authService->authenticate();
if ($authResult->isValid()) {
//......
return $this->redirect()->toRoute('profil',array('action'=>'home'));
} else {
// ......
}
} else {
$messages = $form->getMessages();
}
}
return new ViewModel();
}
}
public function homeAction()
{
if (!$this->identity()) {
return $this->redirect()->toRoute('profil',array('action'=>'signout'));
}
}
public function signoutAction()
{
if ($this->identity()) {
$authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');
$authService->clearIdentity();
}
$this->redirect()->toRoute('profil');
}
,並謝謝你的好意:)
我會試試這個,看看我能否正常工作。 –
你有沒有得到這個工作,你可以分享匹配代碼? –
@IvoJansch:對不起,但目前我不再在這個項目上工作,因爲沒有空閒時間在這個項目上工作...... –