Top Banner
New in CakePHP 3
44

New in cakephp3

Jan 26, 2017

Download

Software

markstory
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: New in cakephp3

New in CakePHP 3

Page 2: New in cakephp3
Page 3: New in cakephp3

March 22, 20153.0.0 is released

Page 4: New in cakephp3

Frequent ReleasesBugfixes every 2-4 weeks

Page 5: New in cakephp3

PHP 5.4 +Soon to be PHP 5.5+

Page 6: New in cakephp3

All the PSRsZero through Four

Page 7: New in cakephp3

Clean upRabid conventions removed.

Page 8: New in cakephp3

Standalone Components

We have a few.

Page 9: New in cakephp3

I18n

Page 10: New in cakephp3

// 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

Page 11: New in cakephp3

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

Page 12: New in cakephp3

msgid "One file removed" msgid_plural "{0} files removed" msgstr[0] "jednom datotekom je uklonjen" msgstr[1] "{0} datoteke uklonjenih" msgstr[2] "{0} slika uklonjenih"

Catalog Files

Page 13: New in cakephp3

use Cake\I18n\Time; use Cake\I18n\Number;

$date = new Time('2015-04-05 23:00:00'); echo $date; >>> 05/04/2015 23:00

echo Number::format(524.23); >>> 524.23

Numbers & Dates

Page 14: New in cakephp3

Locale::setDefault(‘fr-FR’);

$date = new Time('2015-04-05 23:00:00'); echo $date; >>> 5 avril 2015 23:00:00 UTC

echo Number::format(524.23); >>> 524,23

Numbers & Dates

Page 15: New in cakephp3

Use AloneUse the i18n libs anywhere with composer.

Page 16: New in cakephp3

Routing

Page 17: New in cakephp3

Router::scope(‘/u‘, function ($routes) { $routes->connect(‘/name/:username’, [‘controller’ => ‘Users’, ’action’ => ‘show’]); });

// Use namespace prefixed controllers. Router::prefix(‘admin’, function ($routes) { $routes->connect(‘/articles/:action’, [‘controller’ => ‘Articles’]); });

Routing Scopes

Page 18: New in cakephp3

// Classic array format. echo $this->Url->build([ ‘controller’ => ‘Users’, ‘action’ => ‘show’, ‘username’ => ‘thewoz’ ]); >>> /u/name/thewoz

echo $this->Url->build([ ‘prefix’ => ‘Admin’, ‘controller’ => ‘Articles’, ‘action’ => ‘index’ ]); >>> /admin/articles/index

Reverse Routing

Page 19: New in cakephp3

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

Page 20: New in cakephp3

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

Page 21: New in cakephp3

Collections

Page 22: New in cakephp3

Jose Lorenzo Rodriguez

Iterator Master

Page 23: New in cakephp3

ImmutableMutator methods make new collections.

Page 24: New in cakephp3

$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

Page 25: New in cakephp3

$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

Page 26: New in cakephp3

class JoseFinder { public function __invoke($person) { return strtolower($person->first_name) === ‘jose’; } }

$joses = $people->filter(new JoseFinder()); $notBlond = $people->reject(new NotBlondFilter());

Pipeline ++

Page 27: New in cakephp3

Use AloneCollections can be used in any project.

Page 28: New in cakephp3

ORM

Page 29: New in cakephp3

It is not 2005 anymore

Page 30: New in cakephp3

ActiveRecord Datamapper

Page 31: New in cakephp3

// Get a table gateway/mapper. $connection = ConnectionManager::get(‘default’); $articles = new ArticlesTable([‘connection’ => $connection]);

// Basic query building $query = $articles->find() ->where([‘Articles.author_id’ => $userid]) ->order([‘Articles.created’ => ‘DESC’]);

// Find some published, promoted articles $query = $articles->find(‘promoted’) ->find(‘published’);

Finding Records

Page 32: New in cakephp3

// Find articles and eager load relations (1 query) $query = $articles->find() ->contain([‘Authors’, ‘Categories’]);

// Load deeply nested relations (2 queries) $query = $articles->find() ->contain([‘Authors.RecentActivities’]);

Eager Loading

Page 33: New in cakephp3

// 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

Page 34: New in cakephp3

// Do extraction and transformations $result = $articles->find() ->all() ->extract(‘title’) ->map(function ($item) { return strtoupper($item); });

// Extract and reduce $query = $articles->find()->contain([‘Tags’]); $uniqueTags = $articles->all() ->extract(‘tags.{*}.name’) ->reduce(function ($out, $tag) { if (!in_array($tag, $out) { $out[] = $tag; } return $out; }, []);

Collections+

Page 35: New in cakephp3

EntitiesJust vanilla PHP objects for the most part.

Page 36: New in cakephp3

namespace App\Model\Entity;

use Cake\ORM\Entity;

class Article extends Entity { protected $_accessible = [‘title’, ‘body’, ‘author_id’]; }

Article Entity

Page 37: New in cakephp3

namespace App\Model\Entity;

use Cake\ORM\Entity;

class User extends Entity { protected function _getFullName() { return $this->_properties['first_name'] . ' ' . $this->_properties['last_name']; }

}

echo $user->full_name;

Virtual Fields

Page 38: New in cakephp3

Inspired By SQLAlchemy

The best ORM I’ve ever used.

Page 39: New in cakephp3

No Proxies, No Annotations, No Identity Map,

No Runtime Reflection

Page 40: New in cakephp3

No Lazy Loading

Page 41: New in cakephp3

Use aloneUse the ORM anywhere with composer.

Page 42: New in cakephp3

What’s Next?

Page 43: New in cakephp3

What’s Next

• New DateTime library, replacing Carbon

• Polymorphic Associations

• PSR7 Support

• Value Objects

Page 44: New in cakephp3

Thank You.https://joind.in/14774

Twitter - mark_story Github - markstory