Last active
October 14, 2016 22:40
-
-
Save morloderex/a9a59ffcfc20a3e402eaf4a4c56c3629 to your computer and use it in GitHub Desktop.
Very simple flash messinging system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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