Tera
Tera

Reputation: 121

Laravel 5 return view then redirect after

So, I have this function, how do i make it to return 2 views after time passed?

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class emailverified extends Controller
{
  public  function emailverified()
    {
        return view('auth.email.emailverified');
        sleep(3);
        return redirect()->away('https://www.xxx.xxx');
    }
}

It only chose one or the other path. How do i make it return view then after the sleep timer return to another view?

Upvotes: 1

Views: 6546

Answers (4)

Guja1501
Guja1501

Reputation: 1028

You can use header refresh

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class emailverified extends Controller
{
  public  function emailverified()
    {
        return view('auth.email.emailverified')->withHeaders(['refresh' => '3;url=https://www.xxx.xxx']);
    }
}

Upvotes: 2

Mohamed Akram
Mohamed Akram

Reputation: 2117

Its seems you are trying to show a response for the user and redirect to another page in a certain time interval, what you tried is wrong, once your code the return statement in a function, the script will not be further executed and your statement for redirect will not work..

Best solution is, Return the response as ajax response, and you can easily do both action in peralal,

$response = [      
    "ViewBag" =>   view('auth.email.emailverified')->render(),
    'URLTOREDIRECT' => 'https://www.xxx.xxx',
];
return response()->json($response);

Bind the following event to the button or anchor where you want the ajax call happen or if a form submission

$("#yourButtonId").live("click",function(event) {
           var url = '{!!  url("/your/path") !!}';
            $.ajax({
                type: "post",  //depending on your route is get or post
                url: url,
                data: $("#yourformid").serialize()+'&data1='+$('#data1').val()+'&data2='+$('#data2').val()',
                dataType : "json",
                beforeSend:   function(){
                    alert('loading'); //or your can show a user friendly loader
                }
            }).done(function(data) {

                $("#yourDivId").html(data.ViewBag);
                var delay = 1000;  //millisecond
                setTimeout(function(){ window.location = data.URLTOREDIRECT; }, delay);



            }).fail(function(data) {

                if(data.responseJSON){
                    var errors = data.responseJSON;

                    errorsHtml = '<ul>';
                    $.each( errors , function( key, value ) {
                        errorsHtml += '<li>' + value[0] + '</li>';
                    });

                    errorsHtml += '</ul>';

                  alert(errorsHtml);

                }else{
                    alert(data.responseText);
                }

            });

            event.preventDefault();
        });

Upvotes: 1

Niklesh Raut
Niklesh Raut

Reputation: 34924

Try to use javascript inside your controller function

class emailverified extends Controller
{
  public  function emailverified()
    {
        echo "<script>setTimeout(function(){ window.location.href = 'https://www.xxx.xxx'; }, 3000);</script>";
        return view('auth.email.emailverified');

    }
}

Upvotes: 1

kofoworola
kofoworola

Reputation: 557

Instead of returning the view you can try echo view('your view').

The code shouldn't be exited at that point

then add your sleep code and redirect, it should work i just tested it

Upvotes: 0

Related Questions