2

Is there any way that I can, in Laravel 4.2, make a url link such that it sends a DELETE or PUT request?

For example, I have a delete button, I know I could put it in a form and make the form send a delete request to resource.destroy to delete something and all that. What I want is to avoid having o wrap the button in a form, and just make a url that goes to resource.destroy (I'm using restful resources) and does what's in that function. I know I can use action() but that sends a GET request. Can I make it send a DELETE/PUT whatever other request without wrapping it in a form? Is it possible?

2 Answers 2

1

No, it's not possible to send a anything else that a GET request using a "normal" HTML link.
You either have to wrap the button in a form or use javascript. Here's a simple example with jQuery:

The link with generated url

<a id="button" href="{{ route('route.name') }}">Click me</a>

or use the laravel helper for the full link

{{ link_to_route('route.name', 'Click me', array(), array('id' => 'button')) }}

(Actually it doesn't matter how you generate the link as long as the href attribute contains the right url)

jQuery event listener

Then you can listen to the click event, read the href attribute and use it for the ajax request

$('#button').on('click', function(e){
    e.preventDefault(); // avoid redirect from clicking on the link
    $.ajax({
        url: $(this).prop('href'),
        type: 'PUT'
    });
});

Note that this way the user stays on the same page. Depending on what exactly you want to do, you might want to do a redirect after the ajax call

Sign up to request clarification or add additional context in comments.

Comments

1

In addition to @lukasgeiter's answer, another option is to use the little Restfulizer.js "library" that is floating around the web in a few places. Ryan Durham made some updates to it for his Sentinel package, so that's the one I would start off with: restfulizer.js

Using this library, you just add a data-method attribute to the link, and set it to the method you want to use. It uses unobtrusive javascript to create a hidden form with the correct inputs, and submits the hidden form when the link is clicked. You can also set a data-token attribute if you need a CSRF token, and set a class of "action_confirm" if you want a confirm box to pop up (the CSRF token and confirm box are part of Ryan's updates).

Full use example:

<a href="post/1" data-method="delete" data-token="{{ csrf_token() }}" class="action_confirm">Delete Me</a>

A more extreme option would be to include the rails jquery-ujs library (works fine with Laravel), and read the documentation on how to get that working. It would provide a bit more flexibility, but has a higher setup/learning curve.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.