Installation
composer require cody/product-bundle
Publish the bundle's configuration (if applicable):
php artisan vendor:publish --provider="Cody\ProductBundle\ProductServiceProvider"
Service Provider Registration
Ensure the bundle is registered in config/app.php under providers:
Cody\ProductBundle\ProductServiceProvider::class,
First Use Case: Fetching a Product
Inject the ProductService into a controller or service:
use Cody\ProductBundle\Services\ProductService;
class ProductController extends Controller
{
protected $productService;
public function __construct(ProductService $productService)
{
$this->productService = $productService;
}
public function show($id)
{
$product = $this->productService->find($id);
return view('products.show', compact('product'));
}
}
Key Configuration
Check config/product.php for default settings (e.g., API endpoints, caching, or model bindings).
CRUD Operations
Use the ProductService facade or injected service for common actions:
// Create
$product = $this->productService->create([
'name' => 'Laptop',
'price' => 999.99,
'sku' => 'LP-1000'
]);
// Update
$this->productService->update($product->id, ['price' => 899.99]);
// Delete
$this->productService->delete($product->id);
API Integration
If the bundle includes API clients (e.g., for third-party product data), use the ProductApiClient:
$client = app(\Cody\ProductBundle\Clients\ProductApiClient::class);
$externalProducts = $client->fetchProducts(['category' => 'electronics']);
Event Listeners
Listen for product-related events (e.g., ProductCreated, ProductUpdated):
// In EventServiceProvider
protected $listen = [
'Cody\ProductBundle\Events\ProductCreated' => [
'App\Listeners\LogProductCreation',
],
];
Model Bindings
Bind custom models to the bundle’s base Product model:
// In a service provider
$this->app->bind(
\Cody\ProductBundle\Models\Product::class,
\App\Models\CustomProduct::class
);
Validation Rules
Extend or override validation logic via the ProductValidator:
$validator = app(\Cody\ProductBundle\Validators\ProductValidator::class);
$validator->extend('custom_rule', function ($attribute, $value, $parameters) {
// Custom logic
});
Model Conflicts
Product model, ensure your custom model includes all required methods (e.g., getPrice(), getSku()).API Rate Limiting
config/product.php:
'api' => [
'rate_limit' => 100, // Default may be too low
'timeout' => 30,
],
Caching Quirks
php artisan cache:clear
php artisan view:clear
Cache::tags(['products'])->put('product:'.$id, $product);
Event Dispatching
config/app.php.Database Migrations
php artisan migrate --path=/vendor/cody/product-bundle/database/migrations
Enable Debug Mode
Set debug to true in config/product.php to log API calls and service actions:
'debug' => env('APP_DEBUG', false),
Log ProductService Actions
Wrap service calls in a try-catch to log errors:
try {
$product = $this->productService->find($id);
} catch (\Exception $e) {
\Log::error("Product fetch failed for ID {$id}: " . $e->getMessage());
abort(500, 'Product not found');
}
Check for Deprecated Methods
The bundle may use deprecated Laravel features (e.g., Route::resource syntax). Update calls if warnings appear.
Custom Product Attributes
Extend the Product model to add fields:
class CustomProduct extends \Cody\ProductBundle\Models\Product
{
protected $casts = [
'is_featured' => 'boolean',
];
}
Override API Clients Bind a custom client in a service provider:
$this->app->bind(
\Cody\ProductBundle\Clients\ProductApiClient::class,
\App\Clients\CustomProductApiClient::class
);
Add Middleware Attach middleware to product-related routes:
Route::middleware(['auth', 'product.cache'])->group(function () {
Route::resource('products', ProductController::class);
});
How can I help you explore Laravel packages today?