I'm working at getting error messages sent from my plugin to my template (on the front end), when there is no session (no logged in user). The plugin is for LDAP authentication.

Edit: For clarification, this plugin (and the error messages) will be for as-of-yet un-logged-in users. Would a session structure be the only way to get the messages to those end users?

I've tried this (below), but I get the error message (below):

code in the plugin controller: craft()->userSession->setFlash('msg_template',"Credentials were not accepted.")

code in the template:

{% set message = craft.app.session.getFlash('msg_template') %} 

{% if message|length > 0 %}
{{message}} 
{% endif %}

error message:

Neither the property "session" nor one of the methods "session()", "getsession()"/"issession()" or "__call()" exist and have public access in class "Craft\AppVariable

You are mixing Craft 3 code with Craft 2. There is no native way to access the session in Craft 2 via Twig so you need to inject the functions with your plugin

See a working example here

  • Thanks- I added a bit of clarification to my question, above. Would a session be the only way to get error messages to the non-logged-in users? – buck1112 Oct 9 at 19:55
  • Why don't you just work with a proper model as it's intended? $model->addError('name', 'username not found') and in your template user.getErrors() – Robin Schambach Oct 9 at 19:56

Craft 2 doesn't have a craft.app variable you can use like Craft 3 does, but there is a craft.session.getFlash() exposed to the templates in Craft 2 that you can use.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.