diego-campos-fivebyfive/customer-bundle
Installation
composer require kolinalabs/customer-bundle
Verify the package appears in composer.json under require.
Enable the Bundle
Add to config/bundles.php (Laravel 5.4+) or AppKernel.php (Symfony):
Kolina\CustomerBundle\KolinaCustomerBundle::class => ['all' => true],
Define Your Customer Entity
Extend the abstract Customer class in app/Models/Customer.php (Laravel) or src/AppBundle/Entity/Customer.php (Symfony):
namespace App\Models;
use Kolina\CustomerBundle\Entity\Customer as BaseCustomer;
class Customer extends BaseCustomer
{
// Add custom fields/methods (e.g., `protected $phone`)
}
Configure the Bundle
In config/kolina_customer.php (Laravel) or config.yml (Symfony):
kolina_customer:
entity: App\Models\Customer # Laravel path
# entity: AppBundle\Entity\Customer # Symfony path
First Use Case: Create a Customer Inject the manager into a controller/service:
use Kolina\CustomerBundle\Manager\CustomerManager;
class CustomerController extends Controller
{
public function __construct(private CustomerManager $customerManager) {}
public function store(Request $request)
{
$customer = $this->customerManager->create();
$customer->setFirstname($request->firstname);
$this->customerManager->save($customer);
return response()->json($customer);
}
}
CRUD Operations
$manager->create() → Hydrate → $manager->save($customer)$manager->find($id) or $manager->findBy(['email' => '...'])$manager->save($customer)$manager->remove($customer)Integration with FOSUserBundle
Link customers to FOSUser users via setUser():
$customer = $manager->create();
$customer->setUser($fosUserManager->findUserBy(['email' => 'user@example.com']));
$manager->save($customer);
Custom Fields Extend the abstract class and update Doctrine mappings:
// src/App/Models/Customer.php
/**
* @ORM\Column(type="string", length=20)
*/
protected $phone;
Validation Use Symfony’s validator or Laravel’s validation rules:
$validator = $this->validator->validate($customer);
if ($validator->count()) { /* Handle errors */ }
Events
Listen for customer.pre_save/customer.post_save:
// Laravel (EventServiceProvider)
protected $listen = [
'customer.pre_save' => [CustomerEventListener::class, 'onPreSave'],
];
CustomerService) that uses the manager.CustomerManager.CustomerManager in unit tests:
$manager = $this->createMock(CustomerManager::class);
$manager->method('find')->willReturn($customer);
Entity Configuration
config/kolina_customer.php after renaming the Customer class.php artisan cache:clear) and verify the entity path.FOSUserBundle Conflicts
FOSUserBundle is not installed, setUser() will fail.friendsofsymfony/user-bundle or handle the user relation manually.Doctrine Mappings
Service Not Found
kolina_customer.manager not autowired/injected.services.yaml (Laravel) or services.xml (Symfony).Event System
try-catch blocks to log errors:
try {
$manager->save($customer);
} catch (\Exception $e) {
\Log::error("Customer save failed: " . $e->getMessage());
}
postFlush to debug entity state:
$entityManager->getEventManager()->addEventListener(
\Doctrine\ORM\Events::postFlush,
function ($event) {
\Log::debug("Saved entities:", $event->getEntityManager()->getUnitOfWork()->getScheduledEntityInsertions());
}
);
Custom Manager
Extend CustomerManager to add methods:
namespace App\Services;
use Kolina\CustomerBundle\Manager\CustomerManager as BaseManager;
class CustomerManager extends BaseManager
{
public function findByPhone(string $phone): ?Customer
{
return $this->createQueryBuilder('c')
->where('c.phone = :phone')
->setParameter('phone', $phone)
->getQuery()
->getOneOrNullResult();
}
}
Register it as a service alias in config/services.yaml:
Kolina\CustomerBundle\Manager\CustomerManager: '@App\Services\CustomerManager'
Custom Validation
Add constraints to the Customer entity:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\Email()
*/
protected $email;
API Resources
Use Laravel’s ApiResource or Symfony’s serializers to shape responses:
// Laravel (ApiResource)
class CustomerResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->firstname . ' ' . $this->lastname,
'email' => $this->email,
];
}
}
How can I help you explore Laravel packages today?