Redirect after Post - php

After form (method POST) submission i want redirect user to specific page.
usually i used simple line
header("Location: /path/to/redirect/");
exit;
The Zend_Controller_Action have method _redirect example:
$this->_redirect("/path/to/redirect/");
But it have one simple problem: if i refresh page (press F5) last controller action is activated. So its like double post.
Of course i can use old fashion way, but I just want find the zend style redirect.
Edit: p.s after post redirect i want have cleaned form data. Of course i can use own method with header("location:/path") but I searching it implemented in standart zf
Any ideas?

I think thats because _redirect uses an internal redirect. You need to use an external one. You need to use the Redirector action helper directly... in your action:
$this->_redirector->gotoUrlAndExit($url);

Set a session variable that data has been posted, if not post data, redirect?

Related

PaginateViewHelper redirect to request page

I'm using TYPO3\Fluid\ViewHelpers\PaginateViewHelper in my index action which listing some items. Each item has actions which are done on the fly, so after action is called and processed it's back to the index action.
public function deleteAction(Item $item) {
$this->itemService->remove($item);
$this->redirect('index');
}
Unfortunately in this case I'm getting redirected to the first page of index. Is there possibility to redirect to the page where I'm triggering delete action? Can I also obtain arguments of subrequest send to PaginateController?
I know I can use AJAX call instead or write my own pagination, but I'd like to use existing solution if it's possible.
To keep the GET Parameters you can use addQueryString="1" in the LinkViewHelper... maybe this is already enough to keep the current page when using redirect() in the controller.
Otherwise you can use the UriBuilder of the controller to generate a URI including the desired page in your pagination and other parameters and then use the function redirectToUri() to redirect the user to the desired URI.
https://typo3.org/api/typo3cms/class_t_y_p_o3_1_1_c_m_s_1_1_extbase_1_1_mvc_1_1_web_1_1_routing_1_1_uri_builder.html
You can do something like this
$referringRequest = $this->request->getReferringRequest();
if ($referringRequest !== null) {
$this->redirectToRequest($referringRequest);
}
This only works, if the referring request is sent with the original request, which is the case for Fluid Forms, which you should use for unsafe requests (POST, PUT, DELETE).

Clearing $this->input->post()?

How do we clear/reset $this->input->post()? Probably something analogous to clearing $_POST ?
To clear the post use
unset ($_POST);
if it is a must;
create two copies of forms(preferebly in two files in view();
1) every form of type
echo form_input('username','','placeholder="username"')."";
let it be main_form.php;
2) every form of type
echo form_input('username', set_value('username','username'))."";
let it be sub_form.php;
redirect all repeated request to second page
whenever you donot want POST values, then call main_form()
[there might be other ways].
From the page point of view is better to use ajax-request or after a sucesfully form post to redirect to the same page, so if the user refreshes the page will NOT post the form again.
I think something like this redirect(current_url()); (not sure).
This will clear your post and ensure that user don't accidentally double post the data.
Simply call the CI form_validation method.
$this->form_validation->clear_field_data();

Zend, How to access current route from within Action Helper

In Zend Framework I have an Action Helper that loads a login form on most pages. This happens in the preDispatch() method of the Helper and I want to setAction() on the form so that it posts back to the current URL.
What's the best way to access the current URL / route from within the Action Helper? Access the Request (via the Action Controller), then pull then getActionName() and getControllerName(), and concatenate them with baseURL()?
Is there a simpler way? (Set action requires the URI string as a parameter).
Thanks!
You can do as #Elie suggested. However, if you want to use ZF methods for this, you can have a look at this:
$request = Zend_Controller_Front::getInstance()->getRequest();
echo $request->getHeader('referer'); // referer's address
echo $request->getRequestUri(); // current address
I found that I didn't need to access the current URL / route from within the Action Helper. By leaving the form action blank, it automatically posts to the current URL. Perfect.
If I understand correctly, when the user logs in, you want to send them back to the page where they came from. The code I use to do this is:
// the user has come from a particular page - send them back
if($_SERVER['HTTP_REFERER']) {
$this->_redirect($_SERVER['HTTP_REFERER']);
} else {
// the user has come from the home page, or this page
$this->_redirect('/');
}
which is located in the login action (i.e. LoginController->loginAction()).

Different classes and config for AJAX and non-AJAX actions

I have a website where people can place bids on products. So the first thing I have done is created a normal HTML-PHP version of the website which works great.
I have created some basic PHP functions that look at the url -> route you to the controller -> perform the action -> take the POST object etc.
Now I have implemented an AJAX version for this action but I want to know if I am doing the correct thing.
I made a new folder called ajax, and there I made a new controller for the ajax request. Now when somebody clicks on 'bid' then the request is hijacked by Javascript and sent to the AJAX controller. Now in the AJAX controller I strip down the link and then load the controller and model, and the work is done by the class.
Now the response the class has to send back is different for the AJAX version and for the HTML version. Now in the code of the class I have something like this
if(ajax version){
give ajax response
}else{
give html response
}
Now there are more things in the classes that are different.
Now my question is is it ok to use the same classes and config files for the ajax and non ajax function, or do they have to be seperate.
Or is there a more elegant manner of tackling this problem, maybe using interfaces.
If you follow the MVC pattern correctly, which it looks like from your description you are, then you should have a different action per request.
The flow should be
a different URL and action to receive the AJAX request
perform the business logic in your model
the action should then format the response in the style required (AJAX)
By having a switch (the if statement) in a shared controller, you run the risk of adding logic into the controller, which reduces the re-usability of your code.
So, put simply. Have a different PHP page for the different actions.
An easy way to do this is set a GET or POST parameter in the request when using AJAX, such as ajax=1 then check for this in the PHP:
if (isset($_GET['ajax'])){
give ajax response
} else {
give html response
}
AJAX requests have a header HTTP_X_REQUESTED_WITH with a value XMLHttpRequest
For example:
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
// Ajax
} else {
// Normal request
}
As a rule of thumb, it is usually better to keep the same Model and Controller and, based on data in the request (such as a value in the query string, or a custom header such as X-Requested-By), switch Views (probably between an HTML Template View and a JSON View).

Zend_framework - place data in $_POST when using _redirector helper in the controller

I an redirection (in some cases) from a controller to the error controller, action 'not-logged-in' using the redirector helper. My problem is the following: I want to pass an argument in the $_POST array (an URL from where the redirection happened) so the user will be able to return to that page after performing a login.
How can i place data in the $_POST array while using redirect helper?
Thank you ahead.
When you use the redirector with an internal redirect (ie. goToRoute) the paramters are passed along with it. Thus if you add your refferrer to the the request before you actually redirect:
// Assuming $request is a Zend_Controller_Request
$request->setParam('ref', $referrer);
// then use the redirector
then that variable will be passed along with the request upon redirect. So then you would need to check for/grab that variable from the request in the action youve redirected to and then set it as a hidden field in the form. Then when your form posts to your login action you can check again for a ref variable and on successful login redirect to that location.
Now if i were you i would not actually use the referral as the url but a serialized or json encoded array of the previous request's parameters. that way you can use goToRoute in this second instance as well.
Ofcourse if the redirection came form some sort of post action that contained sensitive data you wouldnt want to do this. In that case you would want to use the session as has been previously suggested.
Above all the best advice i can give is to look at the code of Zend_Controller_Router_Rewrite and Zend_Controller_Action_Helper_Redirector.
Not possible without some socket or Curl jiggery pokery.
Why not try using $_SESSION array in the same way?
Does it really matter if the user can see the redirection url in the address bar? i doubt they will care and i see it a few times on some top sites.
Passing control to the login page just feels more like a _forward than a _redirect, like it all belongs under the one action. Especially since you're coming right back.
_forward($action, $controller = null, $module = null, array $params = null)
Then, you can pass your originating location in $params as you'd like.
I'm pretty sure that you can't send POST when redirecting a person to another page. But maybe you can, and if so, I hope somebody proves me wrong here.
I'm not sure how you'd do what you want using Zend Framework, but I would suggest two ways how to do it in general. You can either send a GET variable, or use a session variable to store a back-URL.

Categories