Asked  7 Months ago    Answers:  5   Viewed   26 times

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, i'd like to know if it's a wise design decision to do so.

I've read through the documentation extensively (especially with regards to registering service providers and the manner in which it provides Facades access to entries within the dependency container) - but I can see (and find) no way to replace the IlluminateHttpRequest instance with my own

 Answers

31

Here is Official Document: Request Lifecycle

Content of app/Http/CustomRequest.php

<?php namespace AppHttp;

use IlluminateHttpRequest as BaseRequest;

class CustomRequest extends BaseRequest {
    // coding
}

add this line to public/index.php

$app->alias('request', 'AppHttpCustomRequest');

after

app = require_once __DIR__.'/../bootstrap/app.php';

change the code at public/index.php

IlluminateHttpRequest::capture()

to

AppHttpCustomRequest::capture()
Wednesday, March 31, 2021
 
alez
answered 7 Months ago
36

Since $className is static and within the parent class, when you set className within A or B, it changes the variable within the parent, and the same is done when the variable is read. Unless you override className in your extended classes, you'll be storing and retrieving information from the same memory location, originally defined in InstanceModule.

If you redefine className in A/B, you can access className using parent:: or self:: from InstanceModule or A/B respectively. Depending on what you are trying to do, Abstract classes may also play a significant role.

See Static Keyword or Class Abstraction on the PHP5 Manual.

Wednesday, March 31, 2021
 
Valdas
answered 7 Months ago
33

Recommended approach if you put here only methods (not classes):

  1. Create file anywhere you want
  2. In composer.json make sure you add this file to files key inside autoload like this:

    "autoload": {
        // here other autoload things
    
        "files": ["app/Helpers/AnythingHelper.php"]
    },
    
  3. Run composerdump-autoload`

For classes obviously you should use standard PSR-4 autoloading

Wednesday, March 31, 2021
 
zhartaunik
answered 7 Months ago
85

After re-reading the docs, it is clear that this is intended behaviour. The docs say

As an alternative to mocking, you may use the Event facade's fake method to prevent all event listeners from executing.

As described in a comment, I have to create two tests. One for making sure that the event has been fired and contains the expected data and a second one for making sure that the event listeners get fired.

So, first Test:

Event::fake();

$order = factory(Order::class)->make();

event(new PaymentWasCompleted($order));

Event::assertDispatched(PaymentWasCompleted::class, function ($e) use ($order) {
    return $e->order->id === $order->id;
});

Second test:

Queue::fake();

$order = factory(Order::class)->make();

event(new PaymentWasCompleted($order));

Queue::assertPushed(GenerateInvoiceJob::class, function ($job) use ($order) {
    return $job->order->id === $order->id;
});

As expected, this tests pass.

Saturday, May 29, 2021
 
Amber
answered 5 Months ago
14

In 5.3 the middleware hasn't run yet in the constructor, so you're unable to gather session data. But using your closure-based approach, you should be able to access it with something like this:

$this->middleware(function($request, $next) {
    // Get the session value (uses global helper)
    $this->token = session('_admin_id');

    // If the value is null, abort the request
    if (null === $this->token) abort(404);

    return $next($request);
});
Saturday, May 29, 2021
 
conmen
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :