Skip to content

Pass the response by reference so it can be overwritten in filters #1354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2013
Merged

Pass the response by reference so it can be overwritten in filters #1354

merged 1 commit into from
Jan 5, 2013

Conversation

bayleedev
Copy link

You can edit the response but you can't overwrite it:

<?php
// https://gist.github.com/3896743
$response = new stdClass();

echo '1): ' . spl_object_hash($response) . PHP_EOL; // 0000000021e89fcd00000000e93b17ba

call_user_func_array(function($response) {
    $response = new stdClass();
    echo '2): ' . spl_object_hash($response) . PHP_EOL; // 0000000021e89fcf00000000e93b17ba
}, array($response));

echo '3): ' . spl_object_hash($response) . PHP_EOL; // 0000000021e89fcd00000000e93b17ba

call_user_func_array(function($response) {
    $response = new stdClass();
    echo '4): ' . spl_object_hash($response) . PHP_EOL; // 0000000021e89fcf00000000e93b17ba // hash descoped and reused
}, array(&$response));

echo '5): ' . spl_object_hash($response) . PHP_EOL; // 0000000021e89fcf00000000e93b17ba

Otherwise you'd make the new response object and overwrite the values one at a time:

<?php
// https://gist.github.com/3897032
Route::filter('after', function($response)
{
    $params = \Laravel\Request::$route->parameters;
    // The 'type' is the last param
    // example: /product/(:num).(:any)
    $type = array_pop($params);
    if($type == 'json') {
        $res = Response::json($response->content->data);
        foreach($response as $key => &$value) {
            $response->$key = $res->$key;
        }
    }
});

@Kindari
Copy link
Contributor

Kindari commented Oct 16, 2012

Can you not overwrite the response by returning it? I'm pretty sure you can...

@bayleedev
Copy link
Author

Nope, If you wanted that to happen you'd need to change the third param of Filter::run (route.php:132) from blank to true, then detect if the response is not null and assign it.

I think keeping this the same and just passing it by reference is a better option.

Trace:

  1. Controller
  2. route.php:132
  3. filter.php:119
  4. filter

@JoostK
Copy link
Contributor

JoostK commented Oct 16, 2012

I've also been looking for a way to override the Response from a route (also for the purpose of an API), something like this would be nice.

You can edit the response but you can't overwrite it:
~~~ php
<?php
// https://gist.github.com/3896743
$response = new stdClass();

echo '1): ' . spl_object_hash($response) . PHP_EOL; // 0000000021e89fcd00000000e93b17ba

call_user_func_array(function($response) {
	$response = new stdClass();
	echo '2): ' . spl_object_hash($response) . PHP_EOL; // 0000000021e89fcf00000000e93b17ba
}, array($response));

echo '3): ' . spl_object_hash($response) . PHP_EOL; // 0000000021e89fcd00000000e93b17ba

call_user_func_array(function($response) {
	$response = new stdClass();
	echo '4): ' . spl_object_hash($response) . PHP_EOL; // 0000000021e89fcf00000000e93b17ba // hash descoped and reused
}, array(&$response));

echo '5): ' . spl_object_hash($response) . PHP_EOL; // 0000000021e89fcf00000000e93b17ba
~~~

Otherwise you'd make the new response object and overwrite the values one at a time:
~~~ php
<?php
// https://gist.github.com/3897032
Route::filter('after', function($response)
{
	$params = \Laravel\Request::$route->parameters;
	// The 'type' is the last param
	// example: /product/(:num).(:any)
	$type = array_pop($params);
	if($type == 'json') {
		$res = Response::json($response->content->data);
		foreach($response as $key => &$value) {
			$response->$key = $res->$key;
		}
	}
});
~~~

Signed-off-by: Blaine Schmeisser <blaine.schmeisser@vitals.com>
@Sixthdim
Copy link

+1

taylorotwell added a commit that referenced this pull request Jan 5, 2013
Pass the response by reference so it can be overwritten in filters
@taylorotwell taylorotwell merged commit 6a68734 into laravel:develop Jan 5, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants