dynamitem
dynamitem

Reputation: 1669

Laravel 4 route

I've got a problem with using URL::route. There is a public function in my controller called AuthController called delete_character, here's how it looks:

public function delete_character()
{
  $player->delete();
  return View::make('index')->with('danger', 'You have successfully deleted your character!');
}

Also, I've created a named route:

Route::post('delete_character', array(
           'as'   => 'delete_character', 
           'uses' => 'AuthController@delete_character'
          ));

All I want to do is to execute the $player->delete. I don't want it to be a site, just when I click a button it's gonna delete the player. I've also done the button:

<td><a class="btn btn-mini btn-danger" href="{{ URL::route('delete_character') }}"><i class="icon-trash icon-white"></i> Delete</a></td>

But I constantly get MethodNotAllowedHttpException. Any hints?

Upvotes: 0

Views: 381

Answers (1)

Andreyco
Andreyco

Reputation: 22872

In my example, I am using GET request method (POST is used when form is being submited, for instance) to capture this action. I pass ID of client I wish to delete in the reqeust URL, which results into URL in this form: http://localhost:8888/k/public/admin/client/delete/1 (You should post it from form, according to your example/request).

Not posting whole solution for you to force you to learn! My answer is not 100% identical to your situation, but will help, for sure.

// routes.php
Route::group(['prefix' => 'admin'], function(){
    Route::get('client/delete/{id}', 'Admin\\ClientController@delete');
});

// ClientController.php
<?php

namespace Admin;

use Client;

class ClientController extends BaseController
{
    ...

    public function delete($clientId)
    {
        $client = Client::findOrFail($clientId);

        // $client->delete();

        // return Redirect::back();
    }

    ...

}

// view file, here you generate link to 'delete' action
<a href="{{ URL::action('Admin\\ClientController@delete', ['id' => $client->id]) }}">delete</a>

Upvotes: 1

Related Questions