Welcome Guest, Not a member yet? Register   Sign In
return redirect()->back();
#1

I have a private  error function in my controller that I'm trying to have redirect to the shopping cart.
It sends an email to an errors email address and then I want to have it redirect.
I have tried:
return redirect()->back();
return redirect()->to( '/cart' );
return redirect()->to( base_url( '/cart') );
return redirect()->to( base_url( 'cart') );

None work. The URL in the address bar remains as: <domain>/cart/completeCheckout with a blank page.

Here is the function:

private function _handle_checkout_error( $error, $cart )
{

        helper('common_functions');

        $bmConfig = config('BM');

        session()->setFlashdata('message', 'There was a problem checking out.  Please try again later.');



        $router = service('router');

        $log = "ERROR!\n\n";

        $log .= $error . "\n\n";

        $log .= 'CONTROLLER # METHOD:' . "\n";

        $log .= $router->controllerName() . ' # ' . $router->methodName() . "\n\n";

        $log .= 'GET ARRAY: ' . print_r($_GET, true) . "\n\n";

        $log .= 'POST ARRAY: ' . print_r($_POST, true) . "\n\n";

        $log .= 'CART: ' . print_r($cart, true) . "\n\n";

        $log .= 'USER AGENT:' . "\n";

        $log .= $_SERVER['HTTP_USER_AGENT'];

        log_message('critical', $log);



        email( $bmConfig->emailFromAddress, $bmConfig->errorsEmail, 'ERROR!', $log );



        return redirect()->to( base_url( 'cart' ) );
    }



This code works all the way to the redirect - I get the $log entry and the email, then nothing.
Reply
#2

This should work:
PHP Code:
return redirect()->to'/cart' ); 

...but you already tried it and in didn’t work. That’s weird. Maybe add a log_message() just before the redirect to see if it really get to this point and nothing crashes in the email() call?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

I receive the email, so I know that is working. I’ll add another log entry just before the redirect anyway to check further
Reply
#4

I added another log entry just before the redirect.
So apparently, the code stops executing after the email is sent.
I receive the email, so why would the code execution stop?
Reply
#5

I had been suing my own wrapper function (email) for the PHP mail function and thought that might have something to do with the issue, so I changed the code to use the PHP mail function directly. Same issue - the code execution appears to stop after the PHP mail function is called. Mt statement to write to the log just before the return redirect does not get executed, nor does the return redirect. Any help would be appreciated...
Reply
#6

You need to setup a route for the redirect->to like below

PHP Code:
$routes->add('users/profile''Users::profile', ['as' => 'cart']); 

See the as array on the end, that's what the to looks for in a route. It's not well documented.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

I have autoroute enabled and haven't had to set routes for any other pages - not even the cart the first time it is shown. So, is this really necessary?
Reply
#8

I agree with @includebeer,
PHP Code:
return redirect()->to('/cart'); 
should work, but I also believe that you need to 'finish' the execution:
PHP Code:
return redirect()->to('/cart');
exit; 
Reply
#9

(This post was last modified: 05-15-2021, 05:00 PM by swsupport.)

Adding exit; didn't make any difference.
In reading some more - is it possible that PHP is waiting for a return from the PHP mail function?
Maybe I need to find a way to send the email asynchronously?
I found this code to do that in another search - exec("php mailuser.php > /dev/null 2>&1 &"); - but I am hesitant to use it after reading comments about its security and other issues regarding long-running threads...

I tried doing this instead of the return:

$this->index();

That did work to display the shopping cart - and did send the email, so I'll just use this and quit trying to figure out why the return from a function didn't work.
Reply
#10

If there is a white page problem after redirect, please check your router.  Match would help you. For example :

PHP Code:
$routes->match(['get','post'],'/en/cart''MyController::payment'); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB