baks-dev/contacts-region
BaksDev Contacts Region — модуль для управления региональными контактами (контакты базирования) в PHP 8.4+. Установка через Composer, установка ресурсов командой baks:assets:install, миграции Doctrine, тесты PHPUnit (group=contacts-region).
Install the Package
composer require baks-dev/contacts-region
Publish Configuration and Assets Run the package’s installation command to set up initial configuration and resources:
php artisan baks:assets:install
Note: If using Laravel, ensure the baks:assets:install command is registered in app/Console/Kernel.php or wrapped in an Artisan command.
Run Database Migrations Generate and apply migrations to set up the required tables:
php artisan doctrine:migrations:diff
php artisan doctrine:migrations:migrate
For Laravel users, consider using doctrine/dbal or converting migrations to Laravel’s format.
First Use Case: Create a Regional Contact Use the package’s repository or service to create a contact tied to a region:
use BaksDev\ContactsRegion\Repository\ContactRepository;
$contactRepository = app(ContactRepository::class);
$contact = $contactRepository->create([
'name' => 'John Doe',
'email' => 'john@example.com',
'region_id' => 1, // Assuming region_id is defined in baks-dev/reference-region
'phone' => '+1234567890',
]);
Verify Installation
Check the database for new tables (e.g., contact, region_contact) and test CRUD operations via Tinker or a simple route:
php artisan tinker
>>> $contactRepository->find(1);
ContactRepository or a dedicated service to handle regional contacts. Example:
$service = app(\BaksDev\ContactsRegion\Service\ContactService::class);
$service->createOrUpdate($regionId, $contactData);
$contacts = $contactRepository->findByRegion($regionId, ['name', 'email']);
php artisan baks:assets:install
baks:assets:install):
'asset_paths' => [
'regional_templates' => storage_path('app/regional-templates'),
],
use Illuminate\Database\Eloquent\Model;
abstract class RegionalContactModel extends Model {
protected $table = 'contact';
// Override methods as needed
}
use BaksDev\ContactsRegion\Event\ContactCreated;
event(new ContactCreated($contact));
Note: If using Symfony’s EventDispatcher, wrap it in Laravel’s event system or use a facade.use Illuminate\Validation\Rule;
$validator = Validator::make($data, [
'email' => ['required', Rule::unique('contact')->where('region_id', $regionId)],
]);
$rules = $region->phone_format_rules; // Assume this is fetched from the region
php artisan test --group=contacts-region
public function test_create_regional_contact()
{
$response = $this->post('/contacts', [
'region_id' => 1,
'name' => 'Test Contact',
]);
$response->assertCreated();
}
Service Provider Setup:
Register the package’s services in AppServiceProvider:
public function register()
{
$this->app->bind(
\BaksDev\ContactsRegion\Repository\ContactRepository::class,
\BaksDev\ContactsRegion\Laravel\ContactRepository::class
);
}
Doctrine to Eloquent: Create a Laravel-compatible repository interface:
namespace App\Repositories;
interface ContactRepositoryInterface {
public function findByRegion(int $regionId, array $fields = []);
}
Console Commands: Wrap Symfony commands in Artisan commands. Example:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Application;
class InstallAssetsCommand extends Command {
protected $signature = 'baks:assets:install';
public function handle() {
$symfonyApp = new Application();
$symfonyApp->find('baks:assets:install')->run(new \Symfony\Component\Console\Input\ArrayInput([]), new \Symfony\Component\Console\Output\ConsoleOutput());
}
}
Custom Contact Fields: Add fields via database migrations and update the package’s entity mapper:
// In a migration
Schema::table('contact', function (Blueprint $table) {
$table->string('custom_field')->nullable();
});
// Extend the entity
class ExtendedContact extends \BaksDev\ContactsRegion\Entity\Contact {
public ?string $customField;
}
Regional Contact Search: Add a custom query builder method to the repository:
public function searchByName(string $name, int $regionId): array {
return $this->createQueryBuilder('c')
->where('c.region_id = :regionId')
->andWhere('c.name LIKE :name')
->setParameter('regionId', $regionId)
->setParameter('name', "%{$name}%")
->getQuery()
->getResult();
}
API Endpoints: Create Laravel routes and controllers for regional contacts:
Route::apiResource('regions.{region}/contacts', \App\Http\Controllers\RegionalContactController::class);
baks:assets:install may fail due to missing Symfony dependencies.
Fix: Install the Symfony Console component or wrap commands in Artisan as shown above.symfony/console, symfony/dependency-injection) and Laravel’s dependencies.
Fix: Use composer why-not to diagnose conflicts and override versions in composer.json:
"extra": {
"laravel": {
"dont-discover": ["baks-dev/contacts-region"]
}
}
doctrine/dbal.vendor/baks-dev/contacts-region/migrations/ and adjust or extend them.baks-dev/reference-region for regional data (e.g., region_id).
Fix: Ensure your regional data model matches the package’s expectations. If not, create a mapping layer or extend the reference region package.reference-region package or build a compatibility layer.baks:assets:install command may assume specific file paths (e.g., public/regional-assets).
Fix: Override paths in the package’s configurationHow can I help you explore Laravel packages today?