unleashing creative freedom with modx (2015-07-21 @ php frl)

51
July 2015 Meetup www.php.frl / www.meetup.com/PHP-FRL / @PHPFRL

Upload: mark-hamstra

Post on 14-Aug-2015

464 views

Category:

Internet


1 download

TRANSCRIPT

Page 1: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

July 2015 Meetup

www.php.frl / www.meetup.com/PHP-FRL / @PHPFRL

Page 2: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Echo ‘Hello World!’;www.php.frl / www.meetup.com/PHP-FRL / @PHPFRL

Page 3: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Location Sponsor

Pronamic

www.php.frl / www.meetup.com/PHP-FRL / @PHPFRL

Page 4: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Speaker

Mark Hamstra Unleashing Creative Freedom with MODX

www.php.frl / www.meetup.com/PHP-FRL / @PHPFRL

Page 5: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Unleashing Creative Freedom with

Page 6: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Who am I?Mark Hamstra

Founder & CEA at modmore Freelance MODX Developer

not me

Turbo

Bommel

Page 7: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Agenda

• What is MODX, for whom, available features, how to build a MODX site

• Tour of the MODX Manager (back-end)

• The Architecture of MODX, xPDO ORM, extending and overriding

Page 8: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

MOD-what?

Page 9: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

MOD-what?

• Open Source

Page 10: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

MOD-what?

• Open Source

• Written in PHP (of course)

Page 11: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

MOD-what?

• Open Source

• Written in PHP (of course)

• Already 10 years old young

Page 12: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

MOD-what?

• Open Source

• Written in PHP (of course)

• Already 10 years old young

• Content ManagementSystem Framework Platform

Page 13: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

All the features of a CMS

Page 14: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

All the features of a CMS

rich text editor versioning user groupsmultisite templates multilingual extensionsmarkdown media browser hierarchicalpage tree commercial support automaticmenu builder blogging permissions seofriendly urls server-side caching

Page 15: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Out-of-the-box Install

Page 16: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Out-of-the-box Install

Page 17: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Elements as Building Blocks

TemplatesTemplate Variables

ChunksSnippetsPlugins

Page 18: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Templates

• Usually HTML

• Contains MODX tags

• One template per page

Page 19: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Template Variables

• Custom field for resources

• Commonly “TV”

• Tied to templates

• Text, image, select, checkbox, date, radio, richtext, tag and custom types available

• [[*name-of-tv]]

Page 20: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Chunks

• Usually HTML

• Reusable piece of code

• [[$name-of-chunk]]Template

Chunk “head”

Page 21: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Snippets

• PHP!

• Comparable to a function

• Accepts properties

• [[name-of-snippet]] or [[!name-of-snippet]]

Snippet “helloWorld”

Template

Page 22: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Snippets

Page 23: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

But wait, there’s more!

• [[name-of-snippet]]

• [[!name-of-snippet]]

• = uncached!

• [[++name-of-setting]][[!++name-of-setting]]

• [[$name-of-chunk]][[!$name-of-chunk]]

• [[*name-of-field]][[!*name-of-field]]

Page 24: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

But wait, there’s even more!

• [[helloWorld? &property=`value ̀]]

• [[$head? &extraCss=`<link rel=.. href=..> ̀]]

Page 25: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Plugins

• PHP!

• Event-based, so no tags

• Can read and often influence behaviour

Page 26: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

No need to reinventthe wheel

• Packages (aka extras, add-ons, extensions, third party components…) provide common functionality

• Install via Package Installer inside the manager

Page 27: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Example: getResources

• Lists resources matching conditions

• Uses a Chunk as template

• Use Cases:

• Article listings

• Dynamic (sub)menus

• RSS feed generation

Template

Chunk “blogListItem”

Page 28: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Time for a Manager Tour!http://localhost/tmp/phpfrl/manager/

Page 29: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

MODX Architecture

Page 30: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Secure by Design

cve.mitre.org

Page 31: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Secure by Design

• Automatic $_GET, $_POST, $_REQUEST sanitisation in the request handler

cve.mitre.org

Page 32: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Secure by Design

• Automatic $_GET, $_POST, $_REQUEST sanitisation in the request handler

• xPDO ORM prevents SQL Injections

cve.mitre.org

Page 33: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Secure by Design

• Automatic $_GET, $_POST, $_REQUEST sanitisation in the request handler

• xPDO ORM prevents SQL Injections

• 28 CVE entries, 8 since 2014• WordPress: 906, already ~85 in 2015• Drupal: 915, already ~120 in 2015

cve.mitre.org

Page 34: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

xPDO

Page 35: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

xPDO

• Object Relational Bridge / ORM

Page 36: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

xPDO

• Object Relational Bridge / ORM

• Open Source (modxcms/xpdo)

Page 37: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

xPDO

• Object Relational Bridge / ORM

• Open Source (modxcms/xpdo)

• Extension to PHP’s PDO

Page 38: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

xPDO

• Object Relational Bridge / ORM

• Open Source (modxcms/xpdo)

• Extension to PHP’s PDO

• Support for MySQL, sqlsrv (and more)

Page 39: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Fetching a Single Object

$c = 5;

$obj = $modx->getObject(‘modChunk’, $c);

$c2 = array(‘name’ => ‘head’);

$obj = $modx->getObject(‘modChunk’, $c2)

Page 40: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Easy Query Builder

$c = $modx->newQuery(‘modResource’);

$c->where([

‘parent’ => 0,

‘AND:pagetitle:LIKE => ‘%About%’

]);

$matches = $modx->getCollection(‘modResource’, $c);

foreach ($matches as $modResource) { . . . }

Page 41: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Automatic Filtering

$search = $_POST[‘search’];

$c = $modx->newQuery(‘modResource’);

$c->where([

‘introtext:LIKE’ => “%{$search}%”,

]);

$modx->setPlaceholder(‘search’, sanitise($search));

function sanitise($value) { return htmlentities($value, ENT_QUOTES, ‘UTF-8’); }

👍

Page 42: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Custom Models with xPDO

Page 43: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Custom Models with xPDO

1. Create an xPDO Package Schema (XML)

2. Use build script to write schema into the actual model files/classes

3. Register it before use ($modx->addPackage)

4. Use any xPDO method (getObject, getCollection) on your custom model

Page 44: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

xPDO Package Schema - Head

<?xml version="1.0" encoding="UTF-8"?>

<model package="phpfrl"

baseClass="xPDOSimpleObject"

platform="mysql"

defaultEngine="MyISAM"

version="1.1">

Page 45: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

xPDO Package Schema - Object

<?xml version="1.0" encoding="UTF-8"?>

<model package=“phpfrl” …

<object class="frlMeetup" table=“meetups”>

.. fields ..

</object>

<object class="frlSpeaker" table=“speakers”> … </object>

</model>

Page 46: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

xPDO Package Schema - Fields<?xml version="1.0" encoding="UTF-8"?>

<model package=“phpfrl” …

<object class="frlMeetup" table=“meetups">

<field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default=“PHP FRL Meetup" />

Page 47: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

xPDO Package Schema - Indices

<?xml version="1.0" encoding="UTF-8"?>

<model package=“phpfrl” …

<object class="frlMeetup" table=“meetups"> <field key="name" dbtype=“varchar" … <field key=“starts_on" dbtype=“datetime" <field key="name" dbtype=“varchar" …

<index alias="name" name="name" primary="false" unique="false" type="BTREE"> <column key="name" length="" collation="A" null="false" /> </index> </object>

Page 48: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

xPDO Package Schema - Relations<?xml version="1.0" encoding="UTF-8"?>

<model package=“phpfrl” baseClass=“xPDOSimpleObject" …

<object class="frlMeetup" table=“meetups"> <field key="name" dbtype=“varchar” …>

<composite alias=“Speakers” class=“frlSpeaker” local=“id” foreign=“meetup” cardinality=“many” owner=“local” /> </object>

<object class="frlSpeaker" table=“speakers"> <field key="name" dbtype=“varchar” …> <field key="meetup" dbtype=“int” …>

<aggregate alias=“Meetup” class=“frlMeetup” local=“meetup” foreign=“id” cardinality=“one” owner=“foreign” /> </object>

Page 49: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

xPDO Generated Model

<?phpclass frlMeetup extends xPDOSimpleObject {

}

<?php

class frlMeetup_mysql extends frlMeetup {

}

Page 50: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Interacting with that model$modx->addPackage(‘phpfrl’, ‘/path/to/model/‘);

$c = $modx->newQuery(‘frlMeetup’);$c->sortby(‘starts_on’, ‘DESC’);$meetup = $modx->getObject(‘frlMeetup’, $c);

echo ‘De volgende meetup is ‘ . $meetup->name . ‘ en vind plaats op ‘ . $meetup->starts_on . ‘. ’;

$speakers = $meetup->getMany(‘Speakers’); // or just $meetup->Speakersforeach ($speakers as $spegfytaker) { echo $speaker->name . ‘ zal vertellen over ‘ . $speaker->subject;}

Page 51: Unleashing Creative Freedom with MODX (2015-07-21 @ PHP FRL)

Interesting links: • MODX.com => official website • rtfm.modx.com => official documentation • github.com/modxcms/revolution => source code • MODX.today => daily links/articles about MODX • modmore.com => premium extras for MODX • https://joind.in/14897 => please leave feedback

Enjoy your Creative Freedom