JakeTheBraek
JakeTheBraek

Reputation: 261

Laravel 4 DELETE / PUT being blocked

All of my DELETE and PUT requests get blocked (405). I have tried putting headers in the before and after filters:

App::before(function($request)
{
    header('Access-Control-Allow-Origin', '*');
    header('Allow', 'GET, POST, OPTIONS, PUT, DELETE');
    header('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization, X-Request-With');
    header('Access-Control-Allow-Credentials', 'true');
});


App::after(function($request, $response)
{
    $response->headers->set('Access-Control-Allow-Origin', '*');
    $response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    $response->headers->set('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization, X-Requested-With');
    $response->headers->set('Access-Control-Allow-Credentials', 'true');
    $response->headers->set('Access-Control-Max-Age','86400');
    return $response;
});

I have tried adding .htaccess rules to allow them in the laravel public folder:

<Limit GET POST PUT DELETE HEAD OPTIONS>
    Order allow,deny
    # You might want something a little more secure here, this is a dev setup
    Allow from all
</Limit>
<LimitExcept POST PUT DELETE HEAD OPTIONS>
    Order deny,allow
    Deny from all
</LimitExcept>

I have tried using the constructor in the resource controller and nothing seems to actually allow either request types. I am quickly running out of ideas here, I am using apache and kendo UI making the AJAX requests. My routes look like:

// PUBLIC
Route::group(array('prefix' => 'api/v1'), function()
{
    Route::post('login', array('uses'=>'UsersController@login'));
    Route::get('status', array('uses'=>'UsersController@status'));
});

// PROTECTED BY SENTRY
Route::group(array('prefix' => 'api/v1', 'before' => 'auth'), function()
{
    Route::get('logout', array('uses'=>'UsersController@logout'));
    Route::get('users/whoami', array('uses'=>'UsersController@whoami'));
    Route::resource('users', 'UsersController');
    Route::resource('programs', 'ProgramsController'); // THIS ROUTE KEEPS 405'ING ME
});

Nothing too fancy in the filters files, just using Sentry2 for user management. Any help would be greatly appreciated!

Upvotes: 1

Views: 2858

Answers (1)

JakeTheBraek
JakeTheBraek

Reputation: 261

Nevermind, this is a testament to calling a long night early as you are next to useless if you have worn yourself out.

Basically, you need to keep an eye on the console to be sure you are actually hitting the right endpoint. For example:

1) DELETE localhost/project

versus:

2) DELETE localhost/project/api/v1/endpoint/IDofItem

In the first instance you will get a 405 method not allowed, which made it seem like DELETE was not allowed, but in fact it is saying there is no controller method that corresponds to to the base of your project.

Found the answer in 2 minutes after being fresh in the morning (lesson learned..... again)

Upvotes: 3

Related Questions