flat3 / lodata

The OData v4.01 Producer for Laravel
https://lodata.io/
MIT License
80 stars 27 forks source link

Discovery occurs for all requests, even ones without the Lodata prefix #815

Open John5 opened 6 months ago

John5 commented 6 months ago

While changing code in LodataServiceProvider I noticed that any error there also is thrown on any other route that does not have the Lodata prefix. This means that all the ODP discovery and setup is done for every request. In my case the ODP functionality is being added to an existing monolith so it adds a significant overhead to all requests to existing routes.

I worked around this by adding a check in the boot() method to see if the path of the request matches the configured Lodata prefix like this:

class LodataServiceProvider extends ServiceProvider
{
    public function boot()
    {
        if (!self::isOdpRequest(\Request::path())) {
            return;
        }
        // DO EVERYTHING ELSE
    }

    public static function isOdpRequest(string $path): bool
    {
        $prefix = \Config::get('lodata.prefix');
        return str_starts_with($path, $prefix);
    }
}

I think it is a good idea to add something like this to the Lodata library by default. Otherwise someone else with a similar issue might find this useful.