public function destroyAction() { return $this->respondTo(function ($format) { if (Request::isXhr()) { $resp = $this->jsonResponse(['success' => 'You are now logged out']); } else { $resp = $this->redirectTo('root'); } return $resp->addCookie('dreamer', '', time(), '/'); }); }
public function currentUserAction() { if ($this->currentUser) { return $this->respondTo(function ($format) { if (Request::isXhr()) { return $this->jsonResponse(['id' => $this->currentUser->id, 'username' => $this->currentUser->username]); } }); } else { return $this->show404(); } }
/** * Easily respond to different request types. */ protected function respondTo(callable $callback) { // Is this an XMLHttpRequest? If not, use the request extension or fallback to HTML. $format = Request::isXhr() ? 'js' : Request::$properties->get('extension', 'html'); return $callback($format); }