Skip to content

romanmatyus/flash-messages

 
 

Repository files navigation

Flash Messages

Build Status Scrutinizer Code Quality Latest Stable Version Composer Downloads

Handling system flash messages for Nette Framework

This component replace default flash messages handling. If you want to use one interface for displaying messages, use this extension. For eg. if you are showing messages in modal windows, but sometimes this windows are deactivated, you can reach it with this extension. It store messages in one place and check if were displayed or not.

Installation

The best way to install ipub/flash-messages is using Composer:

{
	"require": {
		"ipub/flash-messages": "dev-master"
	}
}

or

$ composer require ipub/flash-messages:@dev

After that you have to register extension in config.neon.

extensions:
	flashMessages: IPub\FlashMessages\DI\FlashMessagesExtension

Package contains trait, which you will have to use in presenters or components to implement Flash messages component factory. This works only for PHP 5.4+, for older version you can simply copy trait content and paste it into class where you want to use it.

<?php

class BasePresenter extends Nette\Application\UI\Presenter
{

	use IPub\FlashMessages\TFlashMessages;

}

Usage

Create component in Presenter or Control

At first you have to create component as usual, like other component in Nette:

namespace Your\Coool\Namespace\Presenter;

use IPub\FlashMessages;

class SomePresenter
{
	/**
	 * Insert extension trait (only for PHP 5.4+)
	 */
	use FlashMessages\TFlashMessages;

	/**
	 * Component for displaying messages
	 *
	 * @return FlashMessages\Control
	 */
	protected function createComponentFlashMessages()
	{
		// Init action confirm
		$control = $this->flashMessagesFactory->create();

		return $control;
	}
}

Add component to template

Now put this new component into your template:

<document>
<head>
	.....
</head>
<body>
	// ...you template content

	{control flashMessages}

	<div class="content">
		...
	</div>
</body>

Create flash message

And if you want to display some message you can use Nette default method to store flash messages. This default method is overwritten in trait.

<?php

class BasePresenter extends Nette\Application\UI\Presenter
{
	public function actionSome()
	{
		$this->flashMessage('Message text', 'warning');
	}
}

Component will display classic warning message with default template.

Special messages

This extension has its own methods to create flash messages, which support more parameters. You can replace default flashMessage method wit this special:

<?php

class BasePresenter extends Nette\Application\UI\Presenter
{
	public function actionSome()
	{
		$this->flashNotifier->message('Message text', 'warning', 'My message title');

		// or

		$this->warning('Message text', 'My message title');
	}
}

Available methods to store message:

  • $this->flashNotifier->success($messageContent, $messageTitle)
  • $this->flashNotifier->info($messageContent, $messageTitle)
  • $this->flashNotifier->warning($messageContent, $messageTitle)
  • $this->flashNotifier->error($messageContent, $messageTitle)

Message title is optional parameter.

Important modal messages

If you want to create important message and display it in modal window, you can use special option for it: overlay

About

Flash messages handling for presenters & components under Nette

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 92.4%
  • HTML 7.6%