Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincej's avatar
Level 15

When to use Redirect vs View ?

The both appear to do similar things. Yet I have had a problem where using View() was taking me to the correct page, but with a unexpected / weird url. So I changed it to Redirect() and it worked correctly.

I have read on Stackoverflow that redirect should be usEd with POST and view with GET. But that does not make sense to me as my page is working with a redirect on a get route.

So bottom line: What is the difference between Redirect and View and when should you use each one ?

Many Thanks

0 likes
16 replies
mstnorris's avatar

Can you give an example, show some code :)

1 like
vincej's avatar
Level 15

@mstnorris Thanks for coming back ! My big question is not so much how to fix things. Everything is working fine. My question is I don't really know WHY it is working. Going forward I don't want to be guessing as the which one to use Redirect() or View(). I have gone through Jeffrey's lessons and checked Stackoverflow, but can not find a clear explanation.

So - here is the code which brought about the confusion. I hope it helps:

So here is the return statement at the end of a controller. Abbreviated for the sake of clarity:

public function confirm($id )
    {
          Contractor::whereId($id)->update($request->except(['first_name','last_name', 'cellphone_number', 'status', 'hour_rate','square_rate','skill_type','crew',  '_method', '_token']));
      
  return view('labour/contractors/contractor_edit');
    }

I am expecting this return statement to go to the Route:

Route::get('labour/contractors/contractor_edit', 'ContractorEditController@Edit' );

Edit function:

public function edit()
    {
        return View('labour/contractors/contractor_edit');
    }

Ok - So I am getting the correct view, labour/contractors/contractor_edit

But when I look at the url, it is coming off the very same controller which called it in the first place ie public function confirm( ) I can confirm that this is what is happening with a dd(Hello from 'confirm')

Ok - So conclusion is that it is getting the correct view, but it is ignoring the route.

FIX: I change the return statement to:

public function confirm( )
    {
          Contractor::whereId(66)->update($request->except(['first_name','last_name', 'cellphone_number', 'status', 'hour_rate','square_rate','skill_type','crew',  '_method', '_token']));
      
  return redirect ('labour/contractors/contractor_edit');
    }

And it all works correctly, URL is right, and I have the right view, and I have confirmed with a dd() that I have the correct controller.

So when do I use redirect and when view ? I have tons of controllers with return View() and they all seem fine. Obviously I have a gap in my knowledge.

MANY THANKS !! :O)

dmcglone's avatar

Route::get('labour/contractors/contractor_edit', 'ContractorEditController@Edit' );

edit, not Edit

Like this: Route::get('labour/contractors/contractor_edit', 'ContractorEditController@edit' );

mstnorris's avatar

@vincej well, as you mentioned in your original question, it is the norm to return a View when you use a GET request and to Redirect somewhere after you've sent a POST request.

You can of course return a View after a POST request, however, if you think about it, you're done so you just Redirect somewhere as it loads the page or "View" fresh (so to speak). Does that make any sense?

mstnorris's avatar

@dmcglone there aren't any issues with the code. @vincej is asking a theoretical question about the use of Views and Redirects.

1 like
dmcglone's avatar

Wonder how he's getting the route returned using the E.. I must have misunderstood him.

vincej's avatar
Level 15

@dmcglone thanks for your response. Changing the spelling of Edit did not make a difference.

@mstnorris So - understanding you, the inference is that the update statement Contractor::whereId is making a POST and therefore I need to use redirect()

Essentially it is all down to the Update - Correct ???

mstnorris's avatar

I'm sure there are no hard and fast rules, however, it sort of makes sense:

get('/', ... // shows the homepage
get('about', // shows the about page

get('articles/create', // shows the create an article form

post('articles', // persists an article in the database, - sure you could return a view, but you usually redirect to the index page or the new article (show method)
mstnorris's avatar

@vincej does that make sense? And more importantly does it answer your question?

This is of course just my opinion and I could be wrong, but as I mentioned, it does make sense for it to be this way.

vincej's avatar
Level 15

@mstnorris sure - I have long understood the difference between get and post, it is the view vs redirect what throughs me.

Can you please confirm, if redirect is all about POST ie the Update statement uses POST therefore it needs redirect - Correct ??

mstnorris's avatar
Level 55

Yes that is what I believe. That after a POST request, you Redirect to another page.

vincej's avatar
Level 15

Brilliant you are once again a Rockstar .... I'm turning into your best customer :o)

Very much appreciated. Another award point for you !

btw - if I remember correctly you are the guy who is in the NE states, and you are "old" like me, right :o)

I think we talked about BEA and web logic systems last time, right ?

1 like
mstnorris's avatar

You're welcome, ... No, I'm in the UK on the South coast, in Hove to be precise. You mentioned before that you're an expat? I'm 26 ;)

vincej's avatar
Level 15

Oh dear, got that wrong :o)

Yup - I come from Hertfordshire, small town called Hoddesdon. It's about 4 miles south of Hertford Town.

I have been to Hove several times - well, I can't discern between Brighton and Hove. There is a big conference centre / Hotel on the sea front, and I have attended several IT events there. I used to work near Heathrow for several years then later in Bracknell with some of the Big S/W companies.

Well you are good at your job and very generous with your time. I am indebted to you.

btw - I never have understood why people code at 1 am .. bizaar :o)

Many thanks

mstnorris's avatar

Well you're very welcome. I don't really sleep so anytime of day / night makes no odds.

I can never tell the difference between where Hove starts and where Brighton ends either. I think you are talking about the Thistle hotel? That hosts a lot of conferences.

Anyway, glad I could help.

Please or to participate in this conversation.