我正在使用Slim 3框架,twig来管理模板和Eloquent ORM。我的AuthController.php看起来像这样
class AuthController extends Controller{
public function getSignUp($request, $response){
return $this->view->render($response, 'auth/signup.twig');
}
public function postSignUp($request, $response){
$user = User::create([
'firstname' => $request->getParam('first-name'),
'lastname' => $request->getParam('last-name'),
'email' => $request->getParam('email'),
'zipcode' => $request->getParam('zipcode'),
'phonenumber' => $request->getParam('phone-number'),
'username' => $request->getParam('username'),
'password' => password_hash($request->getParam('password'), PASSWORD_DEFAULT)
]);
return $response->withRedirect($this->router->pathFor('home'));
}
}
编写的AJAX函数如下
$(document).ready(function(){
$('#signup').click(function(event){
event.preventDefault();
$.ajax({
type: "post",
url: "{{ path_for('auth.signup') }}",
data: $(this).serialize()
});
});
});
而routes.php有以下两条涉及注册过程的路线
$app->get('/auth/signup', 'AuthController:getSignUp')->setName('auth.signup');
$app->post('/auth/signup', 'AuthController:postSignUp');
现在一切正常。数据通过AJAX正确发布,并插入“用户”表中。但是这段代码
return $response->withRedirect($this->router->pathFor('home'));
不起作用。即使在发布之后,也不会发生重定向。如果表正在更新,这意味着postSignUp()函数中的插入代码正在运行。那么为什么返回语句不起作用?
我对Slim 3框架以及MVC范例相当新,所以我不确定我做错了什么。