I have two things I need to do in Cake 2.2. I need to check that someone is logged in on every page, and then I need to store their user ID as a constant named UID.
I want to avoid being on each controller and having to check the user. I want it done automatically, so naturally I go to AppController and try BeforeFilter. It doesn’t seem to have initialized the session, so I scratch that and try afterFilter, but that (and BeforeRender) is really not what I want to do; I don’t want a single thing that I can stop from happening, to happen on page request if they’re not logged in. Bootstrapping was also not the answer, as far as I could tell.
So how can I check if a user’s logged in, and at the same time define a constant for the rest of my application?
You can do the same by invoking a user session check method in
beforeFilter()method in AppController. Your code should looks like:Kindly ask if it not worked for you.