From 6ed891fc0416e8025f929cf60a07b1020118b221 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Thu, 25 Oct 2018 18:53:05 +0200 Subject: Added logout via AuthController --- src/Controllers/AuthController.php | 36 ++++++++++++++++++++++++++++++++ src/Http/Response.php | 23 ++++++++++++++++++++ src/Http/UrlGeneratorServiceProvider.php | 1 + src/Middleware/LegacyMiddleware.php | 4 ---- 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 src/Controllers/AuthController.php (limited to 'src') diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php new file mode 100644 index 00000000..cdaee167 --- /dev/null +++ b/src/Controllers/AuthController.php @@ -0,0 +1,36 @@ +response = $response; + $this->session = $session; + $this->url = $url; + } + + /** + * @return Response + */ + public function logout() + { + $this->session->invalidate(); + + return $this->response->redirectTo($this->url->to('/')); + } +} diff --git a/src/Http/Response.php b/src/Http/Response.php index 58cd7662..1a7c8209 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -121,4 +121,27 @@ class Response extends SymfonyResponse implements ResponseInterface return $new; } + + /** + * Return an redirect instance + * + * This method retains the immutability of the message and returns + * an instance with the updated status and headers + * + * @param string $path + * @param int $status + * @param array $headers + * @return Response + */ + public function redirectTo($path, $status = 302, $headers = []) + { + $response = $this->withStatus($status); + $response = $response->withHeader('location', $path); + + foreach ($headers as $name => $value) { + $response = $response->withAddedHeader($name, $value); + } + + return $response; + } } diff --git a/src/Http/UrlGeneratorServiceProvider.php b/src/Http/UrlGeneratorServiceProvider.php index 9b9988aa..774e3e35 100644 --- a/src/Http/UrlGeneratorServiceProvider.php +++ b/src/Http/UrlGeneratorServiceProvider.php @@ -11,5 +11,6 @@ class UrlGeneratorServiceProvider extends ServiceProvider $urlGenerator = $this->app->make(UrlGenerator::class); $this->app->instance(UrlGenerator::class, $urlGenerator); $this->app->instance('http.urlGenerator', $urlGenerator); + $this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class); } } diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php index bd3987d2..af2c6a70 100644 --- a/src/Middleware/LegacyMiddleware.php +++ b/src/Middleware/LegacyMiddleware.php @@ -183,10 +183,6 @@ class LegacyMiddleware implements MiddlewareInterface $title = register_title(); $content = guest_register(); return [$title, $content]; - case 'logout': - $title = logout_title(); - $content = guest_logout(); - return [$title, $content]; case 'admin_questions': $title = admin_questions_title(); $content = admin_questions(); -- cgit v1.2.3-54-g00ecf