Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,366 members, 7,954,488 topics. Date: Friday, 20 September 2024 at 08:12 PM |
Nairaland Forum / Science/Technology / Programming / Adding Flash Message To Your Laravel Application Easy (1496 Views)
Message To Nairaland Programmers On Programming and Success / Common Error In Laravel #1:solved File_put_contents Failed To Open Stream / Laravel:call To Undefined Method Illuminate\database\eloquent\collection::save() (2) (3) (4)
(1) (Reply)
Adding Flash Message To Your Laravel Application Easy by dplumptre(m): 12:03pm On Jul 28, 2015 |
Hello peeps, The main idea for this class is to make it easy for you to send flash message without writing too much of code,especially if you are working on an application that requires using flash messages and forms, you are all free to improve on the code or fork this repo, the git repo url is at the end of this page. One thing you must note though , it uses bootstrap 3 css but then you can adjust to make it work for you You can add the FlashClass.php in a services folders in the App folder once that is done you can hook it up in your in your controller , lets look at the flashclass <?php namespace App\Services; use Illuminate\Support\Facades\Session; /** * * * Here we setup for Flash messages and form alert in laravel * * @author Plumptre Ademola <dplumptre@yahoo.com> * * As you can see I am using static functions so you dont have to instanciate just * straight up, check out read me to know how to you display this on the client side * * * * */ class FlashClass { /* This function set() uses two parameters the name of the css class or id the second is the message you want to pass along note i am using bootstrap 3 */ public static function set($css,$message){ Session::flash(‘message’, $message.’!’); Session::flash(‘alert-class’, $css); } /* this function displays the error */ public static function get(){ if(Session::has(‘message’)){ echo”<div class=\”alert “.Session::get(‘alert-class’).”\”> <button type=’button’ class=’close’ data-dismiss=’alert’>×</button> <strong>The following errors have occurred:</strong> <div>”.Session::get(‘message’).” </div> </div>”; } } /* This functions displays errors for the form you are working on normally you will have to ususally write something like this below foreach( $errors->all() as $message ){ echo”<div>”.$message.”</div>”; } echo”</div>”; } but you wont have to worry about that any more because its all here, check readme */ public static function getFormNotice($errors){ if( $errors->count() > 0 ){ echo”<div class=’alert alert-danger’> <button type=’button’ class=’close’ data-dismiss=’alert’>×</button> <strong>The following errors have occurred:</strong>”; foreach( $errors->all() as $message ){ echo”<div>”.$message.”</div>”; } echo”</div>”; } } } ?> The controller page should look like this <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; /* include the namespace of the flashclass */ use App\Services\FlashClass as FlashClass; class WelcomeController extends Controller { public function signup() { return view(‘welcome.sign-up’); } public function postsignup(Requests\SignupRequest $request) { /* using request for the form , using laravel 5.1 here this is how you add it to your controller class below */ FlashClass::set(‘alert-danger’,’there is an error’); return redirect(‘sign-up’); } } } For the view part , this should be kept above the form or where you want the error to show you will have something like this below /* include the name space of the flashclass */use App\Services\FlashClass as FlashClass; /* Above your form you can paste this two line of code FlashClass::getFormNotice($errors); this takes care of the code below foreach( $errors->all() as $message ){ echo”.$message.”; } you might be used to writing which seems easier just writing a one line code This FlashClass::get(); gets the flash message out for */ FlashClass::getFormNotice($errors); FlashClass::get(); you can view on git : https://github.com/dplumptre/flashclass/ Thanks for reading if you have questions or want to see more stuffs visit http://overallheuristic.com/blog/ site : http://overallheuristic.com twitter : @dplumptre fb :https://www.facebook.com/
|
Re: Adding Flash Message To Your Laravel Application Easy by maekhel(m): 12:35pm On Jul 28, 2015 |
Nice one |
(1) (Reply)
A 16 Year Old Nigerian Built A Photo-sharing App... Check It Out! / Hello Programmer Need Ur Assistance / How Did U Make Your First Money From Programming
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13 |