Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8144031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:15:52+00:00 2026-06-06T13:15:52+00:00

My web application as a topbar where i need to display the number of

  • 0

My web application as a topbar where i need to display the number of unreaded messages. Each User entity as an association with Message (many-to-many). Showing the total number of messages (for a given user) would be simple:

class User
{
    /*
     * @ORM\ManyToMany(targetEntity="Message", invertedBy="users")
     */
    private $messages;
}

In Twig:

Total messages: {{ app.user.messages|length }}.

But what if i need to count only new messages? Assuming my repository has a getNewMessages(User $user) method, how can assign this value globally to use in every template?

I know about Twig globals, but i don’t know where i’m supposed to put the relevant code:

$twig = new Twig_Environment($loader);
$twig->addGlobal('text', new Text());

{{ text.lipsum(40) }}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:15:54+00:00Added an answer on June 6, 2026 at 1:15 pm

    I would register an event listener that occurs on every request, probably the kernel.request event. In the listener you can check for a logged in user, and if so update a session variable with the new message count. You can then access this session variable in twig with app.session.msg_count

    This also has the advantage that you can cache it and only update under certain conditions, to avoid querying on every request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Web application is deployed, but hits following error message. I am doing with cxf
My web application sets session every time a user logs in. I checked that
my web application has more than one user with unique usercode. As soon as
My web application requires a user to be logged in to view any webpage
Web Application - > WCF Service - > Sharepoint User Profile Web Service I
My web application will have various types of users: a root user, sys-admins, customers,
In Web application, I'm trying to send mail to the user with an attachment,
My web application allow the user to upload their image to view, and I
My web application allows a user to define from 1 up to 30 emails
My web application is using a number of JQuery plugins such as the Datatables,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.