Browse Source

Lession 29: Session Handling and Flash Messaging

Frederic G. MARAND 6 years ago
parent
commit
99220d936e

+ 1 - 0
app/Http/Controllers/PostsController.php

@@ -100,6 +100,7 @@ class PostsController extends Controller
 
         auth()->user()->publish(new Post(request(['title', 'body'])));
 
+        session()->flash('message', 'Your post has been published.');
         return redirect('/');
     }
 

+ 4 - 1
app/Http/Controllers/RegistrationController.php

@@ -4,6 +4,8 @@ namespace App\Http\Controllers;
 
 use App\Http\Requests\RegistrationForm;
 use App\Mail\Welcome;
+use Illuminate\Session\SessionManager;
+use Illuminate\Session\Store;
 
 class RegistrationController extends Controller
 {
@@ -27,7 +29,8 @@ class RegistrationController extends Controller
 
         \Mail::to($user)->send(new Welcome($user));
 
-
+        $session = session();
+        $session->flash('message', 'Thanks so much for signing up.');
         return redirect()->home();
     }
 }

+ 29 - 0
public/css/app.css

@@ -49,6 +49,35 @@ h6,
   box-shadow: inset 0 -0.1rem 0.25rem rgba(0, 0, 0, 0.1);
 }
 
+#flash-message {
+  position: absolute;
+  bottom: 1ex;
+  right: 1em;
+  z-index: 10;
+}
+
+@-webkit-keyframes flash-message {
+  0% {
+    opacity: 1;
+  }
+
+  100% {
+    opacity: 0;
+    display: none;
+  }
+}
+
+@keyframes flash-message {
+  0% {
+    opacity: 1;
+  }
+
+  100% {
+    opacity: 0;
+    display: none;
+  }
+}
+
 /* Nav links */
 
 .nav-link {

+ 7 - 2
public/js/app.js

@@ -780,7 +780,6 @@ module.exports = __webpack_require__(40);
 /* 9 */
 /***/ (function(module, exports, __webpack_require__) {
 
-
 /**
  * First we will load all of this project's JavaScript dependencies which
  * includes Vue and other libraries. It is a great starting point when
@@ -802,6 +801,12 @@ var app = new Vue({
   el: '#app'
 });
 
+window.setTimeout(function () {
+  $("#flash-message").fadeTo(500, 0).slideUp(500, function () {
+    $(this).remove();
+  });
+}, 3000);
+
 /***/ }),
 /* 10 */
 /***/ (function(module, exports, __webpack_require__) {
@@ -41984,4 +41989,4 @@ if (false) {
 // removed by extract-text-webpack-plugin
 
 /***/ })
-/******/ ]);
+/******/ ]);

+ 6 - 1
resources/assets/js/app.js

@@ -1,4 +1,3 @@
-
 /**
  * First we will load all of this project's JavaScript dependencies which
  * includes Vue and other libraries. It is a great starting point when
@@ -19,3 +18,9 @@ Vue.component('example', require('./components/Example.vue'));
 const app = new Vue({
     el: '#app'
 });
+
+window.setTimeout(function () {
+  $("#flash-message").fadeTo(500, 0).slideUp(500, function () {
+    $(this).remove();
+  });
+}, 3000);

+ 21 - 0
resources/assets/sass/app.scss

@@ -45,6 +45,27 @@ h6, .h6 {
   box-shadow: inset 0 -.1rem .25rem rgba(0,0,0,.1);
 }
 
+#flash-message {
+  position: absolute;
+  bottom: 1ex;
+  right: 1em;
+  z-index: 10;
+
+// Alternative to JS:
+//  animation: flash-message 4s forwards;
+}
+
+@keyframes flash-message {
+  0% {
+    opacity: 1;
+  }
+  100% {
+    opacity: 0;
+    display: none;
+  }
+}
+// end of alternative to JS
+
 /* Nav links */
 .nav-link {
   position: relative;

+ 5 - 0
resources/views/layouts/flash.blade.php

@@ -0,0 +1,5 @@
+@if ($flash = session('message'))
+<div id="flash-message" class="alert alert-success" role="alert">
+    {{ $flash }}
+</div>
+@endif

+ 1 - 0
resources/views/layouts/master.blade.php

@@ -20,6 +20,7 @@
 <body>
     @include('layouts.nav')
     @include('layouts.header')
+    @include('layouts.flash')
 
     <div class="container" id="app">