Installation Add the package via Composer:
composer require codemitte/common
Publish the config file (if available) with:
php artisan vendor:publish --provider="Codemitte\Common\CommonServiceProvider"
First Use Case
Check the src/Helpers directory for immediately usable utility functions. For example, if the package includes string manipulation helpers:
use Codemitte\Common\Helpers\StringHelper;
$snakeCase = StringHelper::snakeCase('CamelCaseString');
Where to Look First
src/Helpers/: Core utility functions (e.g., StringHelper, ArrayHelper).src/Traits/: Reusable traits for models or controllers.src/Exceptions/: Custom exceptions if the package includes them.config/common.php: Configuration options (if published).Helper Functions Use static helper methods for one-off tasks (e.g., formatting, validation):
// Example: Convert array keys to snake_case
$formatted = ArrayHelper::snakeKeys($array);
Traits in Models/Classes Extend Eloquent models or services with traits for reusable logic:
use Codemitte\Common\Traits\HasSoftDeletesWithAudit;
class User extends Model
{
use HasSoftDeletesWithAudit;
}
Middleware or Service Providers
Register package-specific bindings or macros in AppServiceProvider:
public function boot()
{
// Extend Laravel's Str helper
\Str::macro('customMethod', function () {
return \Codemitte\Common\Helpers\StringHelper::customMethod();
});
}
Configuration-Driven Behavior Customize package behavior via config (if supported):
// config/common.php
'default_locale' => 'en_US',
use Codemitte\Common\Rules\CustomRule;
public function rules()
{
return [
'field' => ['required', new CustomRule],
];
}
use Codemitte\Common\Facades\Common;
$result = Common::doSomething();
register():
$this->app->bind(
\App\Contracts\CustomInterface::class,
\Codemitte\Common\Services\CustomService::class
);
$this->partialMock(Codemitte\Common\Services\CustomService::class, ['method']);
Undocumented Features
src/ directly for undocumented helpers.tests/ for usage examples or raise issues for clarification.Namespace Collisions
ArrayHelper vs. Laravel’s Arr).use Codemitte\Common\Helpers\{StringHelper as CMStringHelper};
Configuration Overrides
.env or config/ doesn’t override critical defaults silently.vendor/codemitte/common/config/common.php with your published file.Trait Method Conflicts
boot() in models).as in use statements or override methods:
use Codemitte\Common\Traits\HasSoftDeletesWithAudit as HasAuditTrait;
No Facade Support
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Common extends Facade
{
protected static function getFacadeAccessor() { return 'codemitte.common'; }
}
AppServiceProvider to log helper calls:
if (app()->environment('local')) {
\Codemitte\Common\Helpers\LogHelper::enableDebug();
}
php artisan package:discover to ensure the package is loaded.null, arrays with mixed types).Extend the Package
app/Helpers/ directory to avoid forks:
// app/Helpers/StringHelper.php
namespace App\Helpers;
use Codemitte\Common\Helpers\StringHelper as BaseStringHelper;
class StringHelper extends BaseStringHelper
{
public static function snakeCase(string $string): string
{
return parent::snakeCase($string) . '_custom';
}
}
autoload to include your overrides:
"autoload": {
"files": ["app/Helpers/StringHelper.php"]
}
Performance
$cache = new \Symfony\Component\Cache\Adapter\FilesystemAdapter();
$result = $cache->get('key', function() use ($helper) {
return $helper->expensiveOperation();
});
Type Safety
#[ReturnTypeWillChange]
public static function snakeCase(string $string): string { ... }
Contributing Back
Fallback Logic
if (method_exists(\Codemitte\Common\Helpers\StringHelper::class, 'snakeCase')) {
$result = \Codemitte\Common\Helpers\StringHelper::snakeCase($str);
} else {
$result = Str::snake($str);
}
How can I help you explore Laravel packages today?