Extend Request class in Laravel 5

后端 未结 6 1399
猫巷女王i
猫巷女王i 2020-12-08 22:48

I\'m new to Laravel (only experienced Laravel 5, so no legacy hang up here)

I\'d like to know how to extend the core Request class. In addition to how to extend it,

6条回答
  •  温柔的废话
    2020-12-08 23:17

    I guess you will have to extend also RequestForm. I use trait to avoid code duplication. Code below is relevant for Laravel 5.3.

    app/Http/ExtendRequestTrait.php

    app/Http/Request.php

    app/Http/FormRequest.php

    For phpunit test working you will have to override call method to make it using right Request class here Request::create.

    test/TestCase.php

    app->make('Illuminate\Contracts\Http\Kernel');
    
            $this->currentUri = $this->prepareUrlForRequest($uri);
    
            $this->resetPageContext();
    
            $request = Request::create(
                $this->currentUri, $method, $parameters,
                $cookies, $files, 
                array_replace($this->serverVariables, $server), 
                $content
            );
    
            $response = $kernel->handle($request);
    
            $kernel->terminate($request, $response);
    
            return $this->response = $response;
        }
    
    }
    

    and don't forget to switch Illuminate\Http\Request::capture() to App\Http\Request::capture() in public/index.php file and to add $app->alias('request', 'App\Http\Request'); after or inside $app = require_once __DIR__.'/../bootstrap/app.php';

提交回复
热议问题