How to refresh page
Created: Last updated:
How to refresh or reload a page in Zend Framework? I see this question quite often and the secret is simple but there are a different types of refresh or reload and they require different types of solution.
First we have to find out what type of refresh do you want. For some I already have a document with a solution, hence this document provides answers to some problems but also links to the other existing documents and solution provided there.
To get the full picture and understanding I recommend you check out all links no matter what.
What defines a refresh or reload
First lets find out what type of refresh you are looking for and just so we are clear about the problem I will explain what I consider a refresh or reload. For me it is basically what I see as a user in my browser, i.e. the page gets loaded fresh from the server.
Refresh vs. reload
The most common way for a user is to hit the F5 button to refresh the same page or right-click and then select Refresh in Internet Explorer and Reload in basically all other browsers. We can see right here that we have a difference in terminology. Personally, I like the technical term which is reload but I think the common user prefers refresh over reload.
Force reload client-side
I consider this a client-side solution because a full initial page has to be submitted and loaded by the browser. Then the client based on some information reloads refreshes the page.
This information needed and "unofficial" solution is adding a refresh meta tag to the document head. If the browser supports this meta tag it will act according to the settings which are described following
- // refresh the same page after 10 seconds
- <meta http-equiv="refresh" content="10">
- // reload another page after 5 seconds
- <meta http-equiv="refresh" content="5; url=http://example.com/">
Refresh http-equiv meta with Zend
In terms of Zend Framework there is not much we have to do than add the meta tag into the view like this
- // this goes inside the controller
- // this will reload the same page every 10 seconds
- // this will reload another page after 5 seconds
- $this->view->headMeta()->appendHttpEquiv('refresh','5; url=http://example.com/');
Of course you can manually add or echo the string directly in the layout but I think that you don't want a static solution. The headMeta() method inside a controller is the best way to wrap that inside some condition.
Force reload server-side
We can refresh or reload a page with another feature when we use HTTP and for this we have to introduce a third term: redirect. Because we don't have to send a full page for this redirect I call this a server-side reload.
Usually we want to refresh or reload a webpage from the server-side when we have a form submission, i.e. some POST request. We will send a status code inside the HTTP header which will cause a redirect and forces the client to a GET request. This is commonly known as the PRG (POST/REDIRECT/GET) pattern. For the user this looks like a refresh or reload of the page. Note that this redirect results in the same page but usually it is meant to redirect to another URL and webpage.
For the redirect procedure I already have two documents
The first link is a more basic information about how a redirect works whereas the second document is how to implement it in PHP or the Zend Framework.