Installation Add the package via Composer:
composer require axstrad/common
Publish the config (if applicable) with:
php artisan vendor:publish --provider="Axstrad\Common\CommonServiceProvider"
First Use Case
The package provides utility classes for common Laravel workflows. Start with the Axstrad\Common\Helpers\ArrayHelper for array manipulations:
use Axstrad\Common\Helpers\ArrayHelper;
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$flattened = ArrayHelper::flatten($array); // Returns [1, 2, 3]
Where to Look First
src/ directory for class-level PHPDoc comments.tests/ for usage examples and edge cases.CommonServiceProvider.php for package registration and bindings.Array Manipulation
Use ArrayHelper for nested array operations (e.g., pluck, flatten, dotNotation):
$data = ['user' => ['name' => 'John', 'age' => 30]];
$name = ArrayHelper::get($data, 'user.name'); // "John"
String Utilities
Leverage StringHelper for common string operations:
use Axstrad\Common\Helpers\StringHelper;
$slug = StringHelper::slugify('Hello World'); // "hello-world"
Collection Extensions
Extend Laravel Collections with CollectionHelper:
use Axstrad\Common\Helpers\CollectionHelper;
$collection = collect([1, 2, 3]);
$sum = $collection->sumWithDefault(0); // 6
API Response Handling
Use ApiResponse for consistent API responses:
use Axstrad\Common\Responses\ApiResponse;
return ApiResponse::success(['data' => $data]);
Event Dispatching
Integrate with Laravel events via EventHelper:
use Axstrad\Common\Helpers\EventHelper;
EventHelper::dispatch('user.created', $user);
CommonServiceProvider for reusable logic.Axstrad\Common\Http\Middleware\* for cross-cutting concerns (e.g., logging, validation).Axstrad\Common\Console\Command for CLI tools.Namespace Collisions
The package uses Axstrad\Common namespace. Ensure your project doesn’t shadow this (e.g., avoid App\Common).
Fix: Alias classes in imports or use fully qualified names.
Undocumented Methods Some helper methods lack PHPDoc. Verify behavior via tests or source code:
// Example: Check if `ArrayHelper::mergeRecursive` handles null values as expected.
Configuration Overrides
If publishing config, ensure keys match your .env (e.g., AXSTRAD_COMMON_*).
Collection Macros
Be cautious with CollectionHelper macros—they may conflict with existing Laravel macros.
Tip: Use Collection::macro() to check registered macros before extending.
LogHelper for structured logging:
use Axstrad\Common\Helpers\LogHelper;
LogHelper::debug('Key', $value, ['context' => 'array']);
DumpHelper::dump() includes Laravel’s dd() but with additional formatting.Custom Helpers
Extend Axstrad\Common\BaseHelper to add project-specific utilities:
namespace App\Helpers;
use Axstrad\Common\BaseHelper;
class AppHelper extends BaseHelper {
public static function customMethod() { ... }
}
Service Provider Extensions
Override the provider in your AppServiceProvider:
public function register()
{
$this->app->extend('axstrad.common', function ($service) {
return new CustomCommonService($service);
});
}
Testing
Mock helpers in tests using Laravel’s partialMock:
$mock = $this->partialMock(ArrayHelper::class, ['flatten']);
$mock->shouldReceive('flatten')->andReturn([1, 2, 3]);
config('axstrad-common.defaults'). Override in config/axstrad-common.php:
'defaults' => [
'locale' => 'en_US',
'timezone' => 'UTC',
],
How can I help you explore Laravel packages today?