cakephp where do I call a global function which each page needs - cakephp

I'm using CakePHP (2.2 I think) and am struggling to know where to put a function. It's a function which is called on each page load. The basic logic is
Page is requested
loads page template
loads included header
to check if a cookie has been set.
If cookie is set, use values in that
if cookie hasn't been set, load a value from the database and save in the cookie
save cookie values in a config value to use across the page
continue loading page
Basically the function is to set the config values. I have tried to create a helper which worked nicely as I called it on the header view - but as soon as I try to access the cookie helper it didn't work.
I moved the code in to the app controller and called it on the beforefilter() function, but it seems to get called several times on the one page (even though this does actually all work ok).
So - where would I place a function to do this to get called before ANY html is drawn to the screen and is called only once?
Many thanks,
Matt.

beforeFilter of your AppController.php is the right place. beforeRender should also work, as #zynder mentioned.
beforeFilter should definitely only be called once per request by Cake. If it's working in beforeFilter, but it's being called more than once, you've probably done something wrong, and you should look into that. Or, maybe you could be mistaken in thinking it's executed more than once per request.
Why do you think beforeFilter is being called more than once?

Related

Angular seems to fire on its own

I have a $http.put function that updates my database, this happens on a button click. After I update the database once, and stay on the page, with no interaction at all the update function gets called and will update the database with the last information that was inputted.
I've triple checked my code, there is only one occurrence of my update function being called. And the $http.put is inside $scope.update that is an anonymous function that calls the $http.put.
If I update information and then leave the page, and then come back the function does not fire on it's own.
Here is my Github for the project. I will pull out any specific code that you want to see, but I'm not sure what to show because there are 4 files involved. Those files are
(client/scripts/app.js)
(routes/products.js)
(public/views/routes/adminIndex.html)
(public/views/routes/modProduct.html)
Code is Here
Add a console.trace() just before the $http call to see what is calling it.
You can also use the developper console of your browser and add a breakpoint that will stop the code execution and let you inspect everything (stack trace, current context, etc). Chrome is particularly good at that.

<f:view> beforePhase method called twice when page loads

I am trying to capture url parameters when page is loaded. I am using the beforePhase attribute inside the tag to call the backing bean method which handles the PhaseEvent. I am able to read the parameters but there seems to be some problem. The backing bean method is executed twice. First time I am getting all the url paramters but second time I am getting null.
This is the output in the console I get (The line "BeforePhase ONLOAD" is my SOP statement inside the method. As you can see the SOP is printed twice (below is the console output) -
* <_checkTimestamp> Apache Trinidad is running with time-stamp checking enabled. This should not be used in a production environment. See the org.apache.myfaces.trinidad.CHECK_FILE_MODIFICATION property in WEB-INF/web.xml
beforePhase ONLOAD
<_isBeanValidationAvailable> A Bean Validation provider is not present, therefore bean validation is disabled
beforePhase ONLOAD***
I have to use these url parameters to bind it to the view object and retrieve the data for the page. But if the method is called twice like it has been called here then the second will cause some database error or ultimately i will not see any records on screen.
Is there a way to handle this ?
I actually followed the Approach 1 example seen on this page - http://jneelmani.blogspot.com/2013/01/adf-how-to-call-method-on-page-load.html. In this example, he shows how to avoid beforePhase being called multiple times, I have done that but still the method is called twice.
Any suggestions here will be helpful.
Thanks.
Of course you know ADF supports a mechanism already for bookmarkable pages & URL parameters?
http://myadfnotebook.blogspot.pt/2010/11/bookmarking-with-adf.html

CakePHP: Unsetting request (post) data in the AppController beforeFilter

I'm posting a specific value through a POST form to my CakePHP app. Within the AppController I handle this specific POST data and afterwards I unset $this->request->data, e.g.
if(!empty($this->request->data['Keyboard'])) {
$this->Session->write('Keyboard.active', $this->request->data['Keyboard']['active']);
unset($this->request->data);
}
Afterwards I want ALL request data to be unset (hence the $this->request->data). Within my child controllers I call parent::beforefilter(); as the first line of code in its respective beforeFilter function, making sure the beforeFilter of my appController is initiated. However, in my childController the following line of code will still evaluate to true:
if($this-request->is('post'))
How do I 'unset' the is('post') call? I've also tried $this->request->is('post') = false in the if-statement above, with no success however.
The "problem" with $this->request->is($anything) (in your case at least) is that it is just a function to compare variables, not a setter.
Here's the API code in case you want to check it out.
So, basically, this code compares the variable env('REQUEST_METHOD') to POST (that's in the detectors part of the code).
There's no way a $this->request->is('post') = false is going to work. Something like
$_ENV["REQUEST_METHOD"] = null;
//or
unset($_ENV["REQUEST_METHOD"]);
after you unset the request data might work, but I don't guarantee anything because I haven't tried it.
I feel this approach is very dirty, I don't like to mess with those variables at all if there's no function already available for it, but I expect you to have considered all possible choices to avoid messing with the request already and see this as the only solution. Otherwise you should post another question addressing this subject and see if better approaches come up.
Hopes it helps in your case.

CakePHP: Caching does not work

I am having the following issues: my cakephp app is not handling the cache thing properly. As suggested by every result in google, I created a function in the model to manually delete the cache:
public function afterSave($created) {
Cache::clear();
clearCache();
}
Unfortunately, this is doing nothing. Doesn't delete anything, and I still have the problem.
In case I have no explained myself properly, I will give an example of what happens:
I go with my browser to a page that shows a list of the last 5 records in my database. Then I go and add another record. I come back to the page that shows the last 5, and the information is not updated. It uses the cache and comes back with outdated info. If I press F5, then he page trully reloads and I see the trully 5 last records.
And that's it, I don't know what to do. The whole app works like crap, because you do something and it never appears unless you refresh the page with F5, which is something of course users are unaware, leading them to think "nothing was added" when it actually was.
Cache::clear() will only clear entries that have expired.
Try Cache::clear(FALSE). Works if you have CakePHP 2.x.
I did this to solve the problem: In the controllers, inside beforefilter function I made a check, if the action is something I disable the cache.
The actions you choose won't have browser cache.
function beforeFilter(){
if ($this->action == 'youraction'){
$this->disableCache();
}
}
Use of caching required lots of thinking, where to use where to not. If your update is frequent, don't use caching there.
We use caching where data rarely change, at that moment it is win-win situation.
Cache::clear($check, $config = 'default')
Destroy all cached values for a cache configuration.
cakephp Caching

Sencha Touch store 'aftersync'-like event

I'm using a ListView with a store/ajax-proxy/json-reader/json-writer. I'd like to set the ListView to loading before I call store.sync(), and remove the loading once the response got back from the server.
Problem is, I don't know where I could hook in my call to be processed once the request is done, as all I'm doing is calling sync().
Store has a beforesync event, what I'd like is something like aftersync. Any ideas how to accomplish that?
By reading the Sencha Touch source code, it turns out that the afterRequest function is called just after the request is finished. This config option can be passed to any Ext.data.Proxy subclass. The two arguments passed are request and success.
Interesting though that it's not present in the API docs.

Resources