Skip to content

Instantly share code, notes, and snippets.

@morloderex
Last active October 14, 2016 22:40
Show Gist options
  • Save morloderex/a9a59ffcfc20a3e402eaf4a4c56c3629 to your computer and use it in GitHub Desktop.
Save morloderex/a9a59ffcfc20a3e402eaf4a4c56c3629 to your computer and use it in GitHub Desktop.
Very simple flash messinging system
<?php
namespace App\Http;
use Illuminate\Session\SessionInterface;
use InvalidArgumentException;
class Flash
{
protected $session;
protected static $allowedFunctions = [
'success',
'info',
'warning',
'error'
];
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
public function __call($name, $arguments)
{
if(count($arguments) === 0) {
throw new InvalidArgumentException;
}
if(! in_array($name, static::$allowedFunctions)) {
throw new \Exception("The [{$name}] type is not allowed");
}
return $this->create($arguments[0], $arguments[1], $name);
}
public function overlay($title, $message, $level = 'info')
{
return $this->create($title, $message, $level, 'flash_message_overlay');
}
protected function create($title, $message, $level, $key = 'flash_message')
{
return $this->session->flash($key, [
'level' => $level,
'title' => $title,
'message' => $message
]);
}
}
<?php
function flash($title = null, $message = null)
{
$flash = app('flash');
if(func_num_args() === 0) {
return $flash;
}
return $flash->info($title, $message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment