照明类编辑不会生效

I'm facing a problem in my code and, I think, the easier solution seems to edit an Illuminate Class of the Laravel Framework.

So I opened the file I wanted to edit (public/laravel/framework/src/Illuminate/Http/Response.php) and added a method headers which returns all the response headers :

public function headers()
{
    return $this->headers;
}

But, this edit doesn't seem to be applied because, when I want to use:

Route::filter('cache.put', function($route, $request, $response){
    $headers = $response->headers();
});

the Call to undefined method error is thrown.

My question is : how can I edit a Laravel Class ?

You should not be editing code that belongs to Laravel framework directly. You'll have trouble with updating the framework later on. Even if you managed to make it work the way you're trying, it has the potential to break your whole project since Laravel will still be using it's own Response class, while you try to use your own.

You need to check out the docs about Facades here to do it the Laravel way: http://laravel.com/docs/facades

In short, since the Response class you will be calling is a Facade, you need to replace Laravel's Response facade with one of your own, with your custom facade pointing to your own response class. Something like this:

MyResponseFacade.php:

<?php namespace MyApp;

class MyResponseFacade extends \Illuminate\Support\Facades\Response
{
    public static function make($content = '', $status = 200, array $headers = array())
    {
        return new MyResponse($content, $status, $headers);
    }
}

MyResponse.php

<?php namespace MyApp

class MyResponse extends \Illuminate\Http\Response
{
    public function headers()
    {
        return $this->headers;
    }
}

Then in your app.php, replace:

'Response'        => 'Illuminate\Support\Facades\Response',

with this:

'Response'        => 'MyApp\MyResponseFacade',

And you are done! The whole Laravel app should be returning you Response that is your own class, and you can use the rest of your code with the same name Response.

Make sure you configure the autoload properly, and do php artisan dump-autoload so Laravel can see your new files though.


As a side note, it seems like you are using Laravel by cloning the framework project. Check the answers in here for a better way: Should I download Laravel for every project?