cakephp/core
Core framework utilities for CakePHP: shared classes for configuration, routing, events, cache/logging, error handling, and common helpers used across CakePHP applications and plugins. Provides the foundational building blocks that other CakePHP packages depend on.
Installation
Add the package via Composer (though note this is a read-only split of CakePHP core; use cakephp/cakephp for full functionality):
composer require cakephp/core
For Laravel integration, focus on standalone utilities (e.g., Collection, Utility, I18n) rather than full framework features.
First Use Case: Collections
Leverage CakePHP’s Collection for array manipulation (similar to Laravel’s Collection but with additional methods):
use Cake\Collection\Collection;
use Cake\Collection\CollectionObject;
$data = [['id' => 1, 'name' => 'Alice'], ['id' => 2, 'name' => 'Bob']];
$collection = new Collection(new CollectionObject($data));
// Filter and map like Laravel's Collection
$names = $collection->filter(fn($item) => $item['id'] > 1)
->pluck('name')
->toArray();
Key Entry Points
Cake\Utility\Inflector: For string manipulation (e.g., pluralization, slugs).
use Cake\Utility\Inflector;
echo Inflector::slug('Hello World'); // "hello-world"
Cake\I18n\I18n: For localization (if integrating with Laravel’s translator).Cake\ORM\TableRegistry: Not recommended for Laravel (use Eloquent). Focus on utility classes.Str:: or Carbon with CakePHP’s utilities where needed:
use Cake\Utility\Text;
use Cake\I18n\Time;
// Truncate text
Text::truncate('Long string', 10); // "Long strin..."
// Parse dates
Time::parse('2023-12-31'); // Carbon-like object
Cake\Collection for complex array operations in service layers:
$users = $collection->combine('id', 'name')->toArray();
Service Providers
Register CakePHP utilities as Laravel bindings in AppServiceProvider:
public function register()
{
$this->app->singleton('cake.inflector', fn() => new \Cake\Utility\Inflector());
}
Access via:
app('cake.inflector')->slug('Test');
Middleware/Helpers Create a helper trait for reusable CakePHP logic:
trait CakeHelpers
{
public function cakeSlug(string $string): string
{
return app('cake.inflector')->slug($string);
}
}
TableRegistry: Use Eloquent or Laravel’s query builder.Cake\Utility\Hash for nested data extraction:
use Cake\Utility\Hash;
$value = Hash::get($array, 'user.profile.name');
Framework Lock-In
TableRegistry::get('Users') will fail in Laravel.Namespace Collisions
Cake\ namespace; ensure no conflicts with Laravel’s Cake\ (unlikely, but check autoloading).Performance Overhead
Collection is optimized for CakePHP’s ORM. For large datasets, prefer Laravel’s Collection or raw PHP arrays.Method Not Found?
Verify the class exists in vendor/cakephp/core/src/. Example:
// Works:
use Cake\Utility\Inflector;
// Fails (not in this package):
use Cake\ORM\Table;
Localization Issues
CakePHP’s I18n expects a different config structure than Laravel’s. Use a wrapper:
$translator = new \Cake\I18n\Translator([
'defaultLocale' => 'en_US',
'locale' => 'es_ES',
'sourceLocale' => 'en_US',
]);
Custom Collection Macros
Extend Cake\Collection\Collection with Laravel’s macro system:
\Cake\Collection\Collection::macro('customMethod', function () {
return $this->filter(...);
});
Inflector Rules Add custom pluralization rules:
\Cake\Utility\Inflector::rules('custom', [
'plural' => ['ox' => 'oxen'],
]);
Time Zone Handling
Configure CakePHP’s Time class to use Laravel’s timezone:
\Cake\I18n\Time::setDefaultTimeZone(config('app.timezone'));
$this->partialMock(\Cake\Utility\Inflector::class, [], [], '', false);
How can I help you explore Laravel packages today?