Code Monkey home page Code Monkey logo

f3-flash's Introduction

Flash

This is a little plugin to add simple Flash Messages and Flash Keys for PHP Fat-Free-Framework, version 3.x

Installation

  • Method 1: use composer composer require ikkez/f3-flash

  • Method 2: copy the flash.php file into your F3 lib/ directory or another directory that is known to the AUTOLOADER

Usage

To add a message (or multiple) that should only be displayed once in your template on the next request, just do:

\Flash::instance()->addMessage('You did that wrong.', 'danger');
// or 
\Flash::instance()->addMessage('It worked!', 'success');

And to display that in your templates do:

<!-- bootstrap style-->
<F3:repeat group="{{ \Flash::instance()->getMessages() }}" value="{{ @msg }}">
<div class="alert alert-{{ @msg.status }} alert-dismissable">
  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
  {{ @msg.text | esc }}
</div>
</F3:repeat>

That's it.

If you need, you could also add simple keys:

$flash = \Flash::instance()
$f3->set('FLASH', $flash);
$flash->setKey('highlight','bg-success'); // with value
$flash->setKey('show-hint'); // without returns just TRUE
$flash->setKey('error','Catastrophic error occured! ');

for use cases like:

<div class="box {{ @FLASH->getKey('highlight') }}">
  <F3:check if="{{ @FLASH->getKey('show-hint') }}">
  <p>It's new !!!</p>
  </F3:check>
  ...
</div>
<F3:check if="{{ @@FLASH && @FLASH->hasKey('error') }}">
    <p>{{ @FLASH->getKey('error') }}</p>
</F3:check>

License

You are allowed to use this plugin under the terms of the GNU General Public License version 3 or later.

Copyright (C) 2017 Christian Knuth [ikkez]

f3-flash's People

Contributors

ikkez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

bosoy83 n0nag0n

f3-flash's Issues

Method getMessages() may return null

I don't know if this is intended but,
If you use \Flash::instance()->getMessages() before using \Flash::instance()->addMessage() then getMessages() will return null instead of empty array.
Subsequent invocations of getMessages() return empty array because getMessages() calls clearMessages().

How to close the flash message?

Hello,
Sorry it's more question than an issue I suppose but I would ilke to know what to do to close the Flash Message because the button doesn't do anything.

Regards,
Alain

Error creating CSRF code

i have this code in my base controller's afterroute:
$flash = Flash::instance()->getMessages();
$flash ? $f3->set('FLASH', $flash) : $f3->set('FLASH', 'empty');

and when i try to generate a CSRF code with:
(i have cache on)
$s = new Session();
$f3->CSRF = $s->csrf();

i get this error: (if i comment the 1st code that instances flash, then no error)
session_start(): Failed to read session data: user (path: E:/laragon/tmp)
[E:/laragon/www/base/vendor/bcosca/fatfree-core/base.php:2152] Base->error()
[E:/laragon/www/base/vendor/bcosca/fatfree-core/base.php:273] session_start()
[E:/laragon/www/base/app/inc/Flash.php:31] Base->ref()
[E:/laragon/www/base/vendor/bcosca/fatfree-core/base.php:35] Flash->__construct()
[E:/laragon/www/base/app/controller/BaseController.php:9] Prefab::instance()
[E:/laragon/www/base/vendor/bcosca/fatfree-core/base.php:1782] BaseController->afterroute()
[E:/laragon/www/base/vendor/bcosca/fatfree-core/base.php:1599] Base->call()
[E:/laragon/www/base/public/index.php:32] Base->run()

Forbidden Error

Hey there,

I'm not 100% sure this is a f3-flash issue, but it seems that it's related in some manner. I'm running PHP 7.3.12 and MariaDB 10.3.20 locally. The session and flash messages work 99% of the time. The 1% of the time when it doesn't work, it seems that something odd happens with creating a new session? My local php webserver has been running the whole time, but if a long time passes and then I use it, I get the following error:

Forbidden
HTTP 403 (GET /)

[/project-dir/vendor/bcosca/fatfree/lib/db/sql/session.php:77] Base->error()
[/project-dir/vendor/bcosca/fatfree/lib/base.php:285] session_start()
[/project-dir/vendor/ikkez/f3-flash/lib/flash.php:36] Base->ref()
[/project-dir/vendor/bcosca/fatfree/lib/base.php:35] Flash->__construct()
[index.php:54] Prefab::instance()

On line 30 I setup my db connection, line 36 I setup the new DB session new DB\SQL\Session($f3->db,'sessions',true,null,'CSRF');, and then on line 54 I add the flash instance. $f3->set('flash', \Flash::instance());

This is the code that throws the error ($fw->error(403)) in session.php

<?php
        function read($id) {
		$this->load(['session_id=?',$this->sid=$id]);
		if ($this->dry())
			return '';
		if ($this->get('ip')!=$this->_ip || $this->get('agent')!=$this->_agent) {
			$fw=\Base::instance();
			if (!isset($this->onsuspect) ||
				$fw->call($this->onsuspect,[$this,$id])===FALSE) {
				//NB: `session_destroy` can't be called at that stage (`session_start` not completed)
				$this->destroy($id);
				$this->close();
				unset($fw->{'COOKIE.'.session_name()});
				$fw->error(403);
			}
		}
		return $this->get('data');
	}

I'm just not exactly sure what to fix cause it isn't terribly obvious and going through the code doesn't lead to any clues.

I'm gonna leave some var dumps in there to see if I can figure out if the ip change or the agent changes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.