// Message formatting
echo __("Hello, my name is {0}, I'm {1} years old",
['Sara', 12]);
>>> Hello, my name is Sara, I’m 12 years old
// Decimals and integers
echo __('You have traveled {0,number,decimal}
kilometers in {1,number,integer} weeks',
[5423.344, 5.1]);
>>> You have traveled 5,423.34 kilometers in 5 weeks
Messages
echo __('{0,plural,
=0{No records found}
=1{Found 1 record}
other{Found # records}}',
[1]);
>>> Found 1 record
// Simpler message ids.
echo __('records.found', [1]);
>>> Found 1 record
Plurals
Router::scope(‘/u‘, function ($routes) {
// Explicit name
$routes->connect(‘/friends’, [‘controller’ => ‘Friends’], [‘_name’ => ‘u:friends’]);
});
echo $this->Url->build([‘_name’ => ‘u:friends’]);
>>> /u/friends
Named Routes
Router::scope('/', function ($routes) {
$routes->extensions(['json']);
$routes->resources('Articles');
});
>>> /articles and /articles/:id are now connected.
// Generate nested resources
Router::scope('/', function ($routes) {
$routes->extensions([‘json’]);
$routes->resources('Articles', function ($routes) {
$routes->resources('Comments');
});
});
>>> /articles/:article_id/comments is now connected.
Resource Routing
$items = ['a' => 1, 'b' => 2, 'c' => 3];
$collection = new Collection($items);
// Create a new collection containing elements
// with a value greater than one.
$big = $collection->filter(function ($value, $key, $iterator) {
return $value > 1;
});
// Search data in memory. match() makes a new iterator
$collection = new Collection($comments);
$commentsFromMark = $collection->match(['user.name' => 'Mark']);
Improved Arrays
$people = new Collection($peopleData);
// Find all the non-blondes
$notBlond = $people->reject(function ($p) {
return $p->hair_colour === ‘blond’;
});
// Get all the people named jose
$joses = $notBlond->filter(function ($p) {
return strtolower($p->first_name) === ‘jose’;
});
// Count by their hair colour
$counts = $joses->countBy(function ($p) {
return $p->hair_colour;
});
Pipeline Example
class JoseFinder {
public function __invoke($person) {
return strtolower($person->first_name) === ‘jose’;
}
}
$joses = $people->filter(new JoseFinder());
$notBlond = $people->reject(new NotBlondFilter());
Pipeline ++
// Find all the articles tagged with ‘Cat’
$query = $articles->find()->matching(‘Tags’, function ($q) {
return $q->where([‘Tags.name’ => ‘Cat’]);
});
// Find all the articles without the tag ‘Cat’
$query = $articles->find()->notMatching(‘Tags’, function ($q) {
return $q->where([‘Tags.name’ => ‘Cat’]);
});
Matching