Skip to content

Commit e2dd881

Browse files
committed
feat(service): Implement DotCMS service provider for SDK integration
- Added DotCMSServiceProvider to register DotCMSClient as a singleton. - Refactored AppController to utilize dependency injection for DotCMSClient. - Updated AppServiceProvider to include DotCMSServiceProvider for improved service management.
1 parent 86ac424 commit e2dd881

4 files changed

Lines changed: 53 additions & 15 deletions

File tree

examples/dotcms-laravel/app/Http/Controllers/AppController.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@
44

55
use Illuminate\Http\Request;
66
use Illuminate\Support\Facades\Log;
7-
use Dotcms\PhpSdk\Config\Config;
87
use Dotcms\PhpSdk\DotCMSClient;
98

109
class AppController extends Controller
1110
{
11+
/**
12+
* The DotCMS client instance.
13+
*/
14+
protected $dotCMSClient;
15+
16+
/**
17+
* Create a new controller instance.
18+
*/
19+
public function __construct(DotCMSClient $dotCMSClient)
20+
{
21+
$this->dotCMSClient = $dotCMSClient;
22+
}
23+
1224
/**
1325
* Handle the SPA rendering with dotCMS page data
1426
*
@@ -17,31 +29,22 @@ class AppController extends Controller
1729
*/
1830
public function index(Request $request)
1931
{
20-
// Create dotCMS client configuration
21-
$config = new Config(
22-
host: env('DOTCMS_HOST', 'https://demo.dotcms.com'),
23-
apiKey: env('DOTCMS_API_TOKEN', '')
24-
);
25-
26-
// Create dotCMS client
27-
$client = new DotCMSClient($config);
28-
2932
try {
3033
// Get the current path from the request
3134
$path = $request->path();
3235
$path = $path === '/' ? '/' : '/' . $path;
3336

3437
// Create a page request for the current path
35-
$pageRequest = $client->createPageRequest($path);
38+
$pageRequest = $this->dotCMSClient->createPageRequest($path, 'json');
3639

3740
// Get the page data
38-
$page = $client->getPage($pageRequest);
41+
$page = $this->dotCMSClient->getPage($pageRequest);
3942

4043
// Create a navigation request with depth=2
41-
$navRequest = $client->createNavigationRequest('/', 2);
44+
$navRequest = $this->dotCMSClient->createNavigationRequest('/', 2);
4245

4346
// Get the navigation
44-
$nav = $client->getNavigation($navRequest);
47+
$nav = $this->dotCMSClient->getNavigation($navRequest);
4548

4649
// Pass the data to the view
4750
return view('app', [

examples/dotcms-laravel/app/Providers/AppServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Providers;
44

55
use Illuminate\Support\ServiceProvider;
6+
use App\Providers\DotCMSServiceProvider;
67

78
class AppServiceProvider extends ServiceProvider
89
{
@@ -11,7 +12,7 @@ class AppServiceProvider extends ServiceProvider
1112
*/
1213
public function register(): void
1314
{
14-
//
15+
$this->app->register(DotCMSServiceProvider::class);
1516
}
1617

1718
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Dotcms\PhpSdk\Config\Config;
7+
use Dotcms\PhpSdk\DotCMSClient;
8+
9+
class DotCMSServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Register services.
13+
*/
14+
public function register(): void
15+
{
16+
$this->app->singleton(DotCMSClient::class, function ($app) {
17+
$config = new Config(
18+
host: env('DOTCMS_HOST', 'https://demo.dotcms.com'),
19+
apiKey: env('DOTCMS_API_TOKEN', '')
20+
);
21+
22+
return new DotCMSClient($config);
23+
});
24+
}
25+
26+
/**
27+
* Bootstrap services.
28+
*/
29+
public function boot(): void
30+
{
31+
//
32+
}
33+
}

examples/dotcms-laravel/bootstrap/providers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
return [
44
App\Providers\AppServiceProvider::class,
5+
App\Providers\DotCMSServiceProvider::class,
56
];

0 commit comments

Comments
 (0)