# Service Provider

When Lodata is installed via composer it registers (opens new window) a Service Provider.

This provider sets up an instance of Flat3\Lodata\Model as a singleton in the service container, and configures all the routes and middleware. There are hooks in the configuration to modify the behaviour of the service provider.

View the service provider
<?php

declare(strict_types=1);

namespace Flat3\Lodata;

use Flat3\Lodata\Console\ActionCommand;
use Flat3\Lodata\Console\FunctionCommand;
use Flat3\Lodata\Controller\Monitor;
use Flat3\Lodata\Controller\OData;
use Flat3\Lodata\Controller\ODCFF;
use Flat3\Lodata\Controller\PBIDS;
use Illuminate\Support\Facades\Route;

/**
 * Service Provider
 * @link https://laravel.com/docs/8.x/providers
 * @package Flat3\Lodata
 */
class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
    /**
     * Get the endpoint of the OData service document
     * @return string
     */
    public static function endpoint(): string
    {
        return url(self::route()).'/';
    }

    /**
     * Get the configured route prefix
     * @return string
     */
    public static function route(): string
    {
        return rtrim(config('lodata.prefix'), '/');
    }

    /**
     * Service provider registration method
     */
    public function register()
    {
        $this->mergeConfigFrom(__DIR__.'/../config.php', 'lodata');
    }

    /**
     * Service provider boot method
     */
    public function boot()
    {
        if ($this->app->runningInConsole()) {
            $this->publishes([__DIR__.'/../config.php' => config_path('lodata.php')], 'config');

            $this->commands([
                FunctionCommand::class,
                ActionCommand::class,
            ]);
        }

        $this->app->singleton(Model::class, function () {
            return new Model();
        });

        $this->app->bind('lodata.model', function ($app) {
            return $app->make(Model::class);
        });

        $route = self::route();
        $middleware = config('lodata.middleware', []);

        Route::get("{$route}/_lodata/odata.pbids", [PBIDS::class, 'get']);
        Route::get("{$route}/_lodata/{identifier}.odc", [ODCFF::class, 'get']);
        Route::resource("${route}/_lodata/monitor", Monitor::class);
        Route::any("{$route}{path}", [OData::class, 'handle'])->where('path', '(.*)')->middleware($middleware);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78