Installation
composer require ekyna/google-bundle
Add the bundle to config/bundles.php in your Laravel project:
Ekyna\GoogleBundle\GoogleBundle::class,
Publish Configuration Run the vendor publish command to generate the default config file:
php artisan vendor:publish --provider="Ekyna\GoogleBundle\GoogleServiceProvider" --tag="config"
This creates config/google.php with default settings.
First Use Case: Google API Client Initialization Use the bundle’s service provider to initialize a Google API client in a controller or service:
use Ekyna\GoogleBundle\Services\GoogleClient;
public function __construct(GoogleClient $googleClient) {
$this->googleClient = $googleClient;
}
Inject the GoogleClient class to interact with Google APIs (e.g., Drive, Calendar, etc.).
Service Integration
GoogleClient facade or dependency-injected service to interact with Google APIs.$files = $this->googleClient->drive()->files()->listFiles();
Configuration Management
config/google.php:
'services' => [
'drive' => [
'scopes' => ['https://www.googleapis.com/auth/drive'],
],
'calendar' => [
'scopes' => ['https://www.googleapis.com/auth/calendar'],
],
],
Middleware for Authentication
Route::middleware(['google.auth'])->group(function () {
// Protected routes requiring Google API access
});
Event Handling
event(new \Ekyna\GoogleBundle\Events\TokenRefreshed($token));
GoogleClient in unit tests:
$this->mock(GoogleClient::class)->shouldReceive('drive()->files()->listFiles')->andReturn($mockedFiles);
Missing Configuration
config/google.php is properly set up with valid API credentials (client ID, secret, etc.). Default values may not work out of the box.php artisan vendor:publish --tag="config" and verify the credentials section.Scope Restrictions
config/google.php, API requests will fail with 403 Forbidden.Token Expiry
Google_Auth_Exception in your code:
try {
$result = $this->googleClient->drive()->files()->listFiles();
} catch (\Google_Auth_Exception $e) {
// Log and retry or prompt user to re-authenticate
}
Deprecated Methods
Google_Service_*). Update to newer SDK versions if needed.debug: true in config/google.php to log API requests/responses.config('google.credentials') cautiously.Custom Services
GoogleClient:
class CustomGoogleService {
public function __construct(GoogleClient $client) {
$this->client = $client;
}
public function customMethod() {
return $this->client->drive()->files()->listFiles(['q' => 'custom query']);
}
}
Middleware Customization
app/Http/Kernel.php:
protected $routeMiddleware = [
'google.auth' => \Ekyna\GoogleBundle\Http\Middleware\GoogleAuth::class,
];
Event Listeners
TokenRefreshed) to log or cache tokens:
\Event::listen(\Ekyna\GoogleBundle\Events\TokenRefreshed::class, function ($event) {
\Log::info('Token refreshed', ['token' => $event->token]);
});
How can I help you explore Laravel packages today?