Skip to content

Using in your project

Dylan Anderson edited this page May 13, 2016 · 1 revision

Feedback can be used solely by creating messages and retrieving messages - with no setup needed. It is recommended however, that you add some functionality to your base controller, to reduce some of the overhead that comes with reporting errors to users.

Base controller

Most projects' controllers extend from a base class. One magic method Phalcon provides in its \Phalcon\Mvc\Controller class (which all Phalcon controllers extend) is beforeExecuteRoute(). The code in this method is run before any action method is run. In that method, put this code:

Feedback::setToFlashed();
$this->view->Feedback = new Feedback();

This code imports any messages that were flashed (ie: stored in a session) on the previous page load. It also assigns a view variable of Feedback. Since Feedback is accessed with static methods, you can continue to use Feedback in your action and models, and all generated messages will be available in your view.

Outputing messages

You're free to output Feedback messages in any manner you wish. Messages can be a little cumbersome to output (nature of the beast), so it is recommended to make a partial view that does just that, which you can include whenever you need to output messages. One example implementation in Volt syntax is below.

{% if Feedback.has('success') %}
	<div class = "alert alert-success">
		{% for message in Feedback.getSuccess() %}
			<p>
				{{ message }}
			</p>
		{% endfor %}
	</div>
{% endif %}
{% if Feedback.has('error') %}
	<div class = "alert alert-danger">
		{% for message in Feedback.getError() %}
			<p>
				{{ message }}
			</p>
		{% endfor %}
	</div>
{% endif %}