You are not logged in.

Important Note: This forum is being archived and will be completely locked down on the 7th of February. The new forum can be found at http://laravel.io.

Announcement

    Laravel 4 Advanced Architecture: Book Now On Sale

#1 2012-10-08 06:37:56

Zizaco
Artisan
Registered: 2012-09-14
Posts: 106
Website

Link to access Delete routes in Restful controllers

This is a quick solution that i've found. I think it would be cool to share here.
Based in the rails.js (the default rails javascript the same as this script among other things)

http://paste.laravel.com/b8n

// restfulizer.js

/**
 * Restfulize any hiperlink that contains a data-method attribute by
 * creating a mini form with the specified method and adding a trigger
 * within the link.
 * Requires jQuery!
 *
 * Ex:
 *     <a href="post/1" data-method="delete">destroy</a>
 *     // Will trigger the route Route::delete('post/(:id)')
 * 
 */
$(function(){
    $('[data-method]').append(function(){
        return "\n"+
        "<form action='"+$(this).attr('href')+"' method='POST' style='display:none'>\n"+
        "   <input type='hidden' name='_method' value='"+$(this).attr('data-method')+"'>\n"+
        "</form>\n"
    })
    .removeAttr('href')
    .attr('style','cursor:pointer;')
    .attr('onclick','$(this).find("form").submit();');
});

This way a link created using

{{ HTML::link('thing/1', 'Remove Thing', array( 'data-method' => 'delete')) }}

<!-- or simply -->

<a href="application/thing/1" data-method="delete">Remove Thing</a>

Will actually acces the DELETE route when clicked by the user.

100% Restful friendly (:

Last edited by Zizaco (2013-01-19 13:22:19)

Offline

#2 2012-11-16 10:17:02

markwu
Artisan
Registered: 2012-10-23
Posts: 94
Website

Re: Link to access Delete routes in Restful controllers

It seems a bug  exist in this jquery script, I have to change

$('.[data-model]')....

to

$('[data-model="delete"]')....

to make it work. Anyway, you really save my day. Thanks for sharing.

Last edited by markwu (2012-11-16 10:17:58)

Offline

#3 2012-11-16 11:31:48

juukie14
Artisan
From: Zwartsluis, The Netherlands
Registered: 2012-10-16
Posts: 288

Re: Link to access Delete routes in Restful controllers

Thanks for sharing. It's very usefull smile


Laravel is great! Thanks everybody! :)

Offline

#4 2012-11-16 11:55:20

Zizaco
Artisan
Registered: 2012-09-14
Posts: 106
Website

Re: Link to access Delete routes in Restful controllers

markwu wrote:

It seems a bug  exist in this jquery script, I have to change

to

$('[data-model="delete"]')....

to make it work. Anyway, you really save my day. Thanks for sharing.

You're right. But try

$('[data-model]')....

(without the "dot") instead.
This way you can use it to access PUT routes too. (:

I've fixed the script. Thanks.

Last edited by Zizaco (2012-11-16 11:58:48)

Offline

#5 2012-11-16 12:39:36

markwu
Artisan
Registered: 2012-10-23
Posts: 94
Website

Re: Link to access Delete routes in Restful controllers

Zizaco wrote:
$('[data-model]')....

(without the "dot") instead.
This way you can use it to access PUT routes too. (:

I've fixed the script. Thanks.

Wow ... It's great!!! Thanks.

Mark

Offline

#6 2012-11-16 13:41:49

Anahkiasen
Artisan
From: France
Registered: 2012-07-07
Posts: 241
Website

Re: Link to access Delete routes in Restful controllers

I'm hoping Laravel 4's Glow will handle things like that.

Last edited by Anahkiasen (2012-11-16 13:52:29)

Offline

#7 2012-11-17 04:02:48

markwu
Artisan
Registered: 2012-10-23
Posts: 94
Website

Re: Link to access Delete routes in Restful controllers

Anahkiasen

I don't have time to look into L4 detail yet. What is "Glow" for?

Mark

Offline

#8 2012-11-17 14:32:47

bstrahija
Artisan
From: Croatia
Registered: 2012-08-28
Posts: 158
Website

Re: Link to access Delete routes in Restful controllers

Like it says in the source: JavaScript helper for the Illuminate Framework.

As far as I can tell it's only a URL helper for now, but something like rails.js would be really nice wink


We can exist in ambiguity, but it means the deepest loneliness.
Tutorials: Part 1 | Part 2 | Part 3 | Part 4 | Modules in L4 | Image manipulation
creolab.hr

Offline

#9 2012-12-12 11:50:18

markwu
Artisan
Registered: 2012-10-23
Posts: 94
Website

Re: Link to access Delete routes in Restful controllers

A guy who named 'k_89' in irc channel give the following example, it is another good implementation for restful. Thanks his sharing.

$(function () {
	var makeRequest = function (method, target) {
		if(method === 'GET') {
			window.location.href = target;
			return;
		}

		var html = '<form action="'+target+'" method="POST">';
		html +=    '<input type="hidden" name="_method" value="'+method+'">';
		html +=    '</form>';

		$(html).submit();
	};

	$('a').on('click', function (ev) {
		var $a = $(this);
		if($a.data('method') === 'DELETE') {
			ev.preventDefault();
			
			var answer = confirm("Are you sure?");
			answer && makeRequest($a.data('method'), $a.attr('href'));
		}
	});
});

Original link is http://paste.laravel.com/d9A

Mark

Offline

#10 2013-03-23 23:23:38

rydurham
Artisan
From: Portland, OR
Registered: 2012-12-21
Posts: 8

Re: Link to access Delete routes in Restful controllers

Thanks Andrew - this is exactly what I was looking for!

Offline

#11 2013-07-02 18:38:27

mbariola
Artisan
Registered: 2013-05-29
Posts: 14

Re: Link to access Delete routes in Restful controllers

Hi, sorry to do necroposting, but hope some of the OPs are still around. I tried both solutions but to no avail - the page is just reloaded and the controller not called. No errors are thrown either. I am a noob at javascript and maybe I am overlooking something really trivial or maybe the solutions do not work on Laravel 4.

Can anyone suggest how to properly debug that JS snippet / make sure it is at least called?

Thanks

Offline

#12 2013-10-30 11:07:14

core2kx
Apprentice
Registered: 2013-10-30
Posts: 1

Re: Link to access Delete routes in Restful controllers

This was a great help to me!  Thanks!!!

Offline

#13 2013-11-06 11:56:45

posti85
Artisan
Registered: 2013-11-06
Posts: 10

Re: Link to access Delete routes in Restful controllers

Thanks for sharing. But this way the deletions would be CRSF vulnerables (and also deletions require Javascript). I think it may be better a HTML macro called linkActionDelete() or something like that.

Offline

#14 2014-01-08 18:44:53

elena
Apprentice
From: Brazil
Registered: 2013-12-09
Posts: 4

Re: Link to access Delete routes in Restful controllers

Here's what I got:

/*
|--------------------------------------------------------------------------
| Delete form macro
|--------------------------------------------------------------------------
|
| This macro creates a form with only a submit button. 
| We'll use it to generate forms that will post to a certain url with the DELETE method,
| following REST principles.
|
*/
Form::macro('delete',function($url, $button_label='Delete',$form_parameters = array(),$button_options=array()){

    if(empty($form_parameters)){
        $form_parameters = array(
            'method'=>'DELETE',
            'class' =>'delete-form',
            'url'   =>$url
            );
    }else{
        $form_parameters['url'] = $url;
        $form_parameters['method'] = 'DELETE';
    };

    return Form::open($form_parameters)
            . Form::submit($button_label, $button_options)
            . Form::close();
});

and you call it like this, for example:

{{Form::delete('resource/'. $resource->id, 'Delete')}}

or like this:

{{Form::delete('resource/'. $resource->id, 
                'Delete',
                array('id'=>'the_form_id','class' => 'the-delete-form'),
                array('class'=>'the-delete-link')
                )}}

More details on my blog (blog dot elenakolevska dot com), it's the second post.

Offline

#15 2014-01-08 22:21:26

mbariola
Artisan
Registered: 2013-05-29
Posts: 14

Re: Link to access Delete routes in Restful controllers

elena wrote:

Here's what I got:

[snip]

More details on my blog (blog dot elenakolevska dot com), it's the second post.

Thank you Elena! I worked around the problem, but will try your code as soon as needed.

Offline

#16 2014-01-08 22:23:53

elena
Apprentice
From: Brazil
Registered: 2013-12-09
Posts: 4

Re: Link to access Delete routes in Restful controllers

You're very welcome smile Hope it works for you and if it doesn't, you can easily tweak it...

Offline

Board footer

Powered by FluxBB

');