laravel/laravel — Issue #347: [3.0] Issue with "after" filters
Author: jaysonic
Created: 2012-02-18 09:24:52+00:00
State: open

I'll do my best to explain this...

I have a controller which sets an after filter.

~~~~
$this->filter('after', 'json');
~~~~

This after filter is defined as such:

~~~~
Route::filter('json', function($response)
{
$response->header('Content-Type', 'application/json');
});
~~~~

The problem arises when I have a route pointing to this controller... Take the following URI example: localhost/a/employer/create.json

I have a route setup like so:

~~~~
Route::any('a/(:any)/(:any).(json)', 'ajax.(:1)@(:2)');
~~~~

Now if say my Ajax_Employer_Controller has the follow action:

~~~~
public function get_create()
{
return json_encode(array('foo' => 'bar'));
}
~~~~

When I browse to that page I get the returned content, but the content type is still **text/html**. I've done a bit of testing, and when I change my route to the following:

~~~~
Route::get('a/employer/create.json', array('after' => 'json', function()
{
return json_encode(array('foo' => 'bar'));
}));
~~~~

The content type is now correctly set to the **application/json**.
I've taken a look into the source, it seems that both route filters and controller filters are called. The content type is set fine, but when the route based after filters are run (I'm assuming it's these ones, from my var_dump()'s) the content type is reverted to null, like it's being overridden somewhere.

For now I can do something like `Route::any('a/(:any)/(:any).(json)', array('after' => 'json', 'uses' => 'ajax.(:1)@(:2)'));` although I'm confused as to why the other way wouldn't work.