lionelhenne / laravel-cockpit-cms
A lightweight client for the Cockpit CMS GraphQL API.
Requires
- php: ^8.2
- illuminate/http: ^10.0 || ^11.0 || ^12.0
- illuminate/support: ^10.0 || ^11.0 || ^12.0
README
A simple and lightweight client to query the Cockpit CMS GraphQL API from a Laravel application.
Installation
You can install this package via Composer.
composer require lionelhenne/laravel-cockpit-cms
The Service Provider will be automatically registered thanks to Laravel's package discovery.
Configuration
-
Publish the configuration file
To set your API credentials, you first need to publish the package's configuration file. It will be copied to
config/cockpit.php
.php artisan vendor:publish --tag="cockpit-config"
-
Add your environment variables
Next, open your
.env
file and add the following two keys with your information:COCKPIT_GRAPHQL_ENDPOINT="https://your-cockpit-site.com/api/gql" COCKPIT_API_TOKEN="API-xxxxxxxxxxxxxxxxxxxx"
Usage
The CockpitService
is registered in Laravel's service container. You can simply inject it as a dependency in any controller, job, or service where you need it.
The main method is query()
, which takes a GraphQL string as its first argument and an optional array of variables as its second.
Dependency Injection Example
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use lionelhenne\LaravelCockpitCms\CockpitService; class PageController extends Controller { protected $cockpit; public function __construct(CockpitService $cockpit) { $this->cockpit = $cockpit; } public function __invoke(Request $request) { $query = '{ bannerModel { content } articlesModel(limit: 2, filter: {_state: 1}, sort: {_created: -1}) { _id date tag title excerpt content image } }'; $result = $this->cockpit->query($query); $banner = collect($result['data']['bannerModel'] ?? []); $articles = collect($result['data']['articlesModel'] ?? []); return view('blog.index', compact('banner','articles')); } }
Facade Example
If you prefer, you can use the Cockpit
facade for a more concise syntax.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use lionelhenne\LaravelCockpitCms\Facades\Cockpit; // Import the facade class PageController extends Controller { public function __invoke(Request $request) { $query = '{ bannerModel { content } }'; // Use the facade directly $result = Cockpit::query($query); $banner = collect($result['data']['bannerModel'] ?? []); return view('blog.index', compact('banner')); } } ## License This project is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT). ⚠️ This package was built for my personal needs. It may not be suitable for everyone, and I do not guarantee any support. Use it at your own risk.