简体   繁体   中英

ZF2 Redirect from phtml file

is there a way to make a redirection from inside a phtml file in ZF2 Framework?

The reason is that I am using the jQuery $.POST mechanism (ajax) to refresh a part of the page, but I cannot make a redirection from the controller used by the ajax because it will load the new page into the div (which is inside another page).

I have tried using ZF2's :

$this->_redirect($this->url('restaurants'));

But I get a ServiceNotFoundException. I also tried php's:

<?php header( 'Location: http://www.yoursite.com/new_page.html' ) ; ?>

But this just did not work at all.

Hope this is clear enough!.. Any help is much appreciated!

You can't use header when you printed anything before it. There must not be any output before header . So this means the exact same for $this->_redirect() .

What you could do is the following: Add this to your head section

<meta http-equiv="refresh" content="0; url=http://example.com/">

or use javascript to redirect the user. If you don't want to extend your layout file you should use javascript:

<script type="text/javascript">
    window.location = "http://www.example.com/"
</script>

EDIT

After rereading your question i think the only possible way for you to redirect the user after you've received the page content is the javascript solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM