伙计们正在创建一个Auth模块,在AuthController
内我有三种方法:
1。 loginAction()
2。 autheticateAction()
3。 logoutAction()
问题是,当我提交表单时,autheticate()
方法未执行:
$form->setAttribute('action', $this->url('login/process',array('action' => 'authenticate', )));
例外情况说'我必须对该行动有一个看法':
Zend \ View \ Renderer \ PhpRenderer :: render:无法呈现模板“auth / auth / authenticate”;解析器无法解析为文件
我只需要用于提交数据的流程的方法!!!
在loginAction()
内部我只返回表单和渲染消息,autheticationAction
用于处理数据:
public function loginAction()
{
//if already login, redirect to success page
if ($this->getAuthService()->hasIdentity()){
return $this->redirect()->toRoute('success');
}
$form = $this->getForm();
return array(
'form' => $form,
'messages' => $this->flashmessenger()->getMessages()
);
}
public function authenticateAction()
{
$form = $this->getForm();
$redirect = 'login';
$request = $this->getRequest();
if ($request->isPost()){
$form->setData($request->getPost());
if ($form->isValid()){
//check authentication...
$this->getAuthService()->getAdapter()
->setIdentity($request->getPost('username'))
->setCredential($request->getPost('password'));
$result = $this->getAuthService()->authenticate();
foreach($result->getMessages() as $message)
{
//save message temporary into flashmessenger
$this->flashmessenger()->addMessage($message);
}
if ($result->isValid()) {
$redirect = 'success';
//check if it has rememberMe :
if ($request->getPost('rememberme') == 1 ) {
$this->getSessionStorage()
->setRememberMe(1);
//set storage again
$this->getAuthService()->setStorage($this->getSessionStorage());
}
$this->getAuthService()->getStorage()->write($request->getPost('username'));
}
}
}
return $this->redirect()->toRoute($redirect);
}
这里是我的路线配置:
'login' => array(
'type' => 'Literal',
'options' => array(
'route' => '/auth',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Auth',
'action' => 'login',
),
),
'may_terminate' => true,
'child_routes' => array(
'process' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:action]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
有没有办法不为每个动作定义特定的view
?我真的不需要查看autheticationAction()
堆栈追踪:
#0 D:\xampp\htdocs\AddskProject\vendor\zendframework\zend-view\src\View.php(205): Zend\View\Renderer\PhpRenderer->render(Object(Zend\View\Model\ViewModel))
#1 D:\xampp\htdocs\AddskProject\vendor\zendframework\zend-view\src\View.php(233): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#2 D:\xampp\htdocs\AddskProject\vendor\zendframework\zend-view\src\View.php(198): Zend\View\View->renderChildren(Object(Zend\View\Model\ViewModel))
#3 D:\xampp\htdocs\AddskProject\vendor\zendframework\zend-mvc\src\View\Http\DefaultRenderingStrategy.php(103): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#4 [internal function]: Zend\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Zend\Mvc\MvcEvent))
#5 D:\xampp\htdocs\AddskProject\vendor\zendframework\zend-eventmanager\src\EventManager.php(444): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#6 D:\xampp\htdocs\AddskProject\vendor\zendframework\zend-eventmanager\src\EventManager.php(205): Zend\EventManager\EventManager->triggerListeners('render', Object(Zend\Mvc\MvcEvent), Array)
#7 D:\xampp\htdocs\AddskProject\vendor\zendframework\zend-mvc\src\Application.php(353): Zend\EventManager\EventManager->trigger('render', Object(Zend\Mvc\MvcEvent))
#8 D:\xampp\htdocs\AddskProject\vendor\zendframework\zend-mvc\src\Application.php(328): Zend\Mvc\Application->completeRequest(Object(Zend\Mvc\MvcEvent))
#9 D:\xampp\htdocs\AddskProject\public\index.php(21): Zend\Mvc\Application->run()
#10 {main}