Skip to content

Instantly share code, notes, and snippets.

@JoDeveloper
Forked from willishq/Flash.php
Created August 18, 2019 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoDeveloper/17a5b1300bbcb1379b15601fa7a38970 to your computer and use it in GitHub Desktop.
Save JoDeveloper/17a5b1300bbcb1379b15601fa7a38970 to your computer and use it in GitHub Desktop.
Laravel 5 Flash messages inspired by Laracasts Flash, optimized for Zurb Foundation

Laravel 5 Flash Message Helper

Inspired by Laracasts Flash

I've made an Actual Repo for this now, you can install it via composer too:

composer require willishq/laravel5-flash

Usage

Ideally use within your base controller as so:

namespace App\Http\Controllers;


use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use App\Services\Flash;
use Illuminate\View\Factory;

abstract class Controller extends BaseController {

	use DispatchesCommands, ValidatesRequests;
	/**
	 * @var Flash
	 */
	protected $flash;
	public function __construct(Factory $view, Flash $flash)
	{
		$this->flash = $flash;
		$view->share('flash', $flash);
	}
}

To fire off a flash message:

class FooController extends BaseController {

	public function somethingNeat()
	{
	    // epic codes
	    $this->flash->success('success message');
	    return redirect('/');
	}
}

To display flash messages in your view:

@if($flash->exists())
	@if($flash->isPanel())
		<div class="panel">
			<h5>{{ $flash->title }}</h5>
			<p>{{ $flash->message }}</p>
		</div>
	@else
		<div data-alert class="alert-box {{ $flash->type }}">
			<p>{{ $flash->message }}</p>
			<a href="#" class="close">&times;</a>
		</div>
	@endif
	
@endif
<?php namespace App\Services;
use Illuminate\Session\Store;
/**
* Laravel 5 Flash messages inspired by Laracasts Flash Messages.
*
* @link https://github.com/laracasts/flash
*
* @author Andrew Willis <andrew@willishq.co.uk>
*
*
*/
class Flash {
/**
* @var string
*/
public $message;
/**
* @var string
*/
public $title;
/**
* @var string
*/
public $type;
/**
* @var Store
*/
protected $session;
/**
* @var string
*/
protected $namespace = 'flash_message';
/**
* @var bool
*/
protected $exists = false;
public function __construct(Store $session) {
$this->session = $session;
if ($this->session->has($this->namespace)) {
$flashed = $this->session->get($this->namespace);
$this->message = $flashed['message'];
$this->title = $flashed['title'];
$this->type = $flashed['type'];
$this->exists = true;
}
}
/**
* Determine whether flash data exists on this request.
* @return bool
*/
public function exists()
{
return $this->exists;
}
/**
* Setup the flash messsage data.
*
* @param string $message
* @param string $title
* @param string $level
*/
protected function message($message, $title = '', $type = 'info')
{
$this->message = $message;
$this->title = $title;
$this->type = $type;
$this->session->flash($this->namespace, (array) $this);
}
/**
* Magic method to create error messages from type or check if a message is a type.
*
* <code>
* $flash->error('error message', 'title');
* $flash->success('success message', 'title');
* $flash->overlay('message', 'title');
*
* $flash->isError(); // bool
* $flash->isPanel(); // bool
* $flash->isOverlay(); //bool
* </code>
*
* @param string $type
* @param array $params
*/
public function __call($type, $params)
{
if (count($params) === 0 && strpos($type, 'is') === 0) {
$type = strtolower(str_replace('is', '', $type));
return $this->type === $type;
} else if (count($params) === 1) {
$this->message($params[0], '', $type);
} else if (count($params) === 2) {
$this->message($params[0], $params[1], $type);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment