Issues (63)

Branch: master

app/Providers/RoutesServiceProvider.php (1 issue)

Severity
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Providers;
4
5
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
6
use Illuminate\Routing\Middleware\SubstituteBindings;
7
use Illuminate\Routing\Router;
8
use Illuminate\Session\Middleware\StartSession;
9
use Illuminate\Support\Facades\Route;
10
use Illuminate\Support\ServiceProvider;
11
use Illuminate\View\Middleware\ShareErrorsFromSession;
12
use Thinktomorrow\Chief\Admin\HealthMonitor\Middleware\MonitorMiddleware;
13
use Thinktomorrow\Chief\App\Http\Middleware\AuthenticateChiefSession;
14
use Thinktomorrow\Chief\App\Http\Middleware\ChiefAdminLocale;
15
use Thinktomorrow\Chief\App\Http\Middleware\ChiefNavigation;
16
use Thinktomorrow\Chief\App\Http\Middleware\ChiefRedirectIfAuthenticated;
17
use Thinktomorrow\Chief\App\Http\Middleware\ChiefValidateInvite;
18
use Thinktomorrow\Chief\App\Http\Middleware\EncryptCookies;
19
use Thinktomorrow\Chief\Shared\AdminEnvironment;
20
use Thinktomorrow\Chief\Site\Urls\ChiefResponse;
21
22
class RoutesServiceProvider extends ServiceProvider
23
{
24
    public function boot(): void
25
    {
26
        $this->autoloadFrontendRoute();
27
        $this->loadOpenAdminRoutes();
28
29
        if (app(AdminEnvironment::class)->check(request())) {
30
            $this->loadAdminRoutes();
31
            $this->autoloadAdminMiddleware();
32
        }
33
    }
34
35
    private function autoloadFrontendRoute()
36
    {
37
        if (true !== config('chief.route.autoload')) {
38
            return;
39
        }
40
41
        app()->booted(function () {
0 ignored issues
show
The method booted() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        app()->/** @scrutinizer ignore-call */ booted(function () {
Loading history...
42
            $routeName = config('chief.route.name');
43
44
            Route::get('{slug?}', function ($slug = '/') {
45
                return ChiefResponse::fromSlug($slug);
46
            })->name($routeName)
47
                ->where('slug', '(.*)?')
48
                ->middleware('web');
49
        });
50
    }
51
52
    private function loadOpenAdminRoutes(): void
53
    {
54
        Route::group(['prefix' => config('chief.route.prefix', 'admin'), 'middleware' => ['web']], function () {
55
            $this->loadRoutesFrom(__DIR__ . '/../../routes/chief-open-routes.php');
56
        });
57
    }
58
59
    private function loadAdminRoutes(): void
60
    {
61
        Route::group(['prefix' => config('chief.route.prefix', 'admin'), 'middleware' => ['web-chief', 'auth:chief']], function () {
62
            $this->loadRoutesFrom(__DIR__ . '/../../routes/chief-admin-routes.php');
63
64
            // Add project specific chief routing...
65
            $projectChiefRoutePath = config('chief.route.admin-filepath', null);
66
67
            if ($projectChiefRoutePath && file_exists($projectChiefRoutePath)) {
68
                $this->loadRoutesFrom($projectChiefRoutePath);
69
            }
70
        });
71
    }
72
73
    private function autoloadAdminMiddleware(): void
74
    {
75
        app(Router::class)->middlewareGroup('web-chief', [
76
            // The default laravel web middleware - except for the csrf token verification.
77
            EncryptCookies::class,
78
            AddQueuedCookiesToResponse::class,
79
            StartSession::class,
80
            ShareErrorsFromSession::class,
81
            SubstituteBindings::class,
82
83
            // Chief admin specific middleware
84
            AuthenticateChiefSession::class,
85
            MonitorMiddleware::class,
86
            ChiefNavigation::class,
87
            ChiefAdminLocale::class,
88
        ]);
89
90
        app(Router::class)->aliasMiddleware('chief-guest', ChiefRedirectIfAuthenticated::class);
91
        app(Router::class)->aliasMiddleware('chief-validate-invite', ChiefValidateInvite::class);
92
    }
93
}
94