How to change the method of an HTTP request in Laravel 4

Asked

Viewed 476 times

8

I need to perform CRUD operations in certain locations of my application that are contained within other Formulars. For this I will use AJAX to update the views containing the "sub-content'.

I’m using the methods of Resource Controllers of Laravel 4 to control routes from standard Framework methods:

Actions Resource Controller

To better illustrate the scenario: There is a register of companies that has n people assigned to this company.

The view cadastro.empresas.edit has a form with company data and within this view I have a div that will work people being updated by AJAX according to CRUD operations.

The problem: When I make a return Redirect::action('PessoasController@show', array('id' => $pessoa->id)); I need the requisition method to be a GET, however as the original request (update) is a PUT I end up falling into an infinite loop.

How do I indicate which method I want to use when calling a Redirect::action() ?

2 answers

1


It seems that your problem is not the possibility because the method

Redirect::route('rota.pessoa.show, $pessoa->id);

Works normal here in my 4.1 Standard Applications, your question there is that you are trying to make a redirect in an AJAX request, who would be waiting for only one answer.

Two tips, either you return an "OK" to this request, and after receiving the OK you make the call of the show method, or else you make this implementation, using something like Angularjs or Backbonejs.

  • 1

    Really @Allan Freitas, for lack of knowledge insisted on waiting for Redirect. I changed the code so it works the way you suggested and it’s working. Now I just need to let it more "lean", because I am not very comfortable in leaving this kind of thing on the frontend. Anyway thank you very much!

0

Try to use Redirect::route('rota.pessoa.show, $pessoa->id)

Makes it easier to maintain and read.
I always name my routes, even not using Resource, and never had a similar problem with the scenario you indicated.

  • Thanks for the reply @vinicius73, but had already tried to use named route. Analyzing the requests it sends the redirect as PUT. I am using: if (Request::ajax()){return Redirect::route('cadastro.pessoas.show', $pessoa->id);} in the controller. Any other suggestions?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.