SlideShare a Scribd company logo
RESOURCE 
ROUTING 
IN EE 
@michaelrog 
#EEconf 2014
RESOURCE 
ROUTING FOR 
TRIPLE-DIGIT 
GROWTH!!!!!!1 
@michaelrog 
#EEconf 2014
WELCOME
WHAT IS RESOURCE 
ROUTING? 
The stage in a request where the server 
and/or application decides what 
resource to provide, and in what form, 
based on the URL, info in the request, 
and the characteristics of the server 
environment.
WHAT IS RESOURCE 
ROUTING? 
The stage in a request where the server 
and/or application decides what 
resource to provide, and in what form, 
based on the URL, info in the request, 
and the characteristics of the server 
environment.
WHAT IS RESOURCE 
ROUTING? 
This happens when… 
• Apache gives you a file 
• Apache gives you a file based on an .htaccess 
rewrite/directive 
• Wikipedia decides what article to give you 
• an API decides what controller to instantiate 
• EE decides what template to process
HOW DOES EE ROUTE 
URLS TO TEMPLATES?
WHY WOULD WE 
WANT TO CHANGE IT? 
To break out of the group/template scheme 
• Templates should be organized by content type, 
not content values 
• Hierarchical content 
• Ambiguous/conflicting segments 
To add pre-routing functionality 
• Business logic should live in the app 
• Templates are views 
Performance
THIS IS (USUALLY) BAD: 
{if segment 2 == "category"} 
{embed="blog/.category"} 
{if:elseif segment_2 == "view"} 
{embed="blog/.single"} 
{if:else} 
{embed="blog/.listing"} 
{/if}
HOW CAN WE CHANGE IT?
NATIVE TEMPLATE ROUTES
NATIVE TEMPLATE ROUTES 
• One route per template 
• Can contain static or dynamic segments 
/products/boots 
/products/{product_name:alpha_dash} 
• Segments can contain multiple variables 
/{month:integer}-{day:integer}-{year:integer} 
/{date:regex[(d{2}-d{2}-d{4})]} 
• Segments can be bound by multiple rules 
{variable:rule1|rule2[arg0, arg1, ...]|...}
NATIVE TEMPLATE ROUTES 
• Order matters 
/events/{type:alpha_dash} 
/events/{date:regex[(d{2}-d{2}-d{4})]} 
• Include All Segments 
/name/{first_name:alpha}/{last_name:alpha}/ 
{suffix:regex[(i|v|x)+]} 
/name/{first_name:alpha}/{last_name:alpha} 
/name/{first_name:alpha}
EXTENSION HOOKS
SESSIONS_END 
$this->extensions->call('sessions_end', $this); 
if ($this->extensions->end_script === TRUE) 
{ 
return; 
}
SESSIONS_END 
public sessions_end($sess) { 
$redirect = ... ; 
$url = ... ; 
if ($redirect) { 
$this->EE->session = $sess; 
$this->EE->functions->redirect($url); 
} 
return true; 
}
CORE_TEMPLATE_ROUTE HOOK 
$edata = ee()->extensions 
->call('core_template_route’, 
ee()->uri->uri_string); 
if (is_array($edata) && count($edata) == 2) 
{ 
list($template_group, $template) = $edata; 
}
CORE_TEMPLATE_ROUTE HOOK 
public sessions_end($uri) { 
$template1 = ... ; 
$template2 = ... ; 
$where_to_go = (... ? $template1 : $template2); 
return array( 
'template_group', // Template group name 
'template' // Template name 
); 
}
ADD-ONS THAT DO THIS 
• Structure 
— http://buildwithstructure.com/ 
• Overrides front-end routes based on hierarchical URIs 
• Overrides CP routes based on optional Settings 
• Resource Router 
— https://github.com/rsanchez/resource_router 
• Remap URI routes to an HTTP response, using 
CodeIgniter-style routings
STRUCTURE 
Use case: 
• Entries as pages in a hierarchy or taxonomy.
STRUCTURE 
Each Channel has a default template for its entries, and a 
default behavior: Page or Listing
STRUCTURE
STRUCTURE 
Structure even hijacks CP routes a bit: I can make Structure 
my CP homepage, and redirect to it after an entry is saved.
RESOURCE ROUTER 
Use cases: 
• to break out of the template_group/template_name 
paradigm of traditional EE routing 
• to nest URLs on a Structure/Pages URI (pagination, 
categories, etc.) 
• to point multiple URL patterns to a single template 
• to add custom JSON/HTML endpoints that do not warrant 
a full EE template 
• to remove excess conditional logic in your templates
RESOURCE ROUTER 
$config['resource_router'] = array( 
'uri/pattern' => ... 
)
RESOURCE ROUTER 
{if segment 2 == "category"} 
{embed="blog/.category"} 
{if:elseif segment_2 == "view"} 
{embed="blog/.single"} 
{if:else} 
{embed="blog/.listing"} 
{/if}
RESOURCE ROUTER 
$config['resource_router'] = array( 
'blog/:any' => function ($router, $wildcard) { 
if ($wildcard->value == 'category') { 
$router->setTemplate('blog/.category'); 
} elseif ($wildcard->value === 'view') { 
$router->setTemplate('blog/.single'); 
} else { 
$router->setTemplate('blog/.listing'); 
} 
} 
)
RESOURCE ROUTER 
Wildcards 
• :any 
• :num 
• :year 
• :month 
• :day 
• :pagination 
• :category 
• :page:XX 
• :all
RESOURCE ROUTER 
$config['resource_router'] = array( 
'blog/:category' => 'site/blog-category', 
'blog/:year' => 'site/blog-yearly-archive', 
);
RESOURCE ROUTER 
Validating Wildcards 
• :entry_id 
• :url_title 
• :category_id 
• :category_url_title 
• :member_id 
• :username
RESOURCE ROUTER 
Callbacks 
• $router->setTemplate(string $template) 
• $router->set404() 
• $router->setGlobal(string $key, $value) 
• $router->setVariable(string $key, mixed $value) 
• {exp:resource_router:your_var_name} 
• $router->setHeader(string $name, string $value) 
• $router->json(mixed $data) 
• $router->redirect(string $url, int $statusCode)
RESOURCE ROUTER 
Wildcard functions 
• $wildcard->isValidEntryId(array $where) 
• $wildcard->isValidUrlTitle(array $where) 
• $wildcard->isValidEntry(array $where) 
• $wildcard->isValidCategoryId(array $where) 
• $wildcard->isValidCategoryUrlTitle(array $where) 
• $wildcard->isValidCategory(array $where) 
• $wildcard->isValidMemberId(array $where) 
• $wildcard->isValidUsername(array $where) 
• $wildcard->getMeta(string $column)
CASE STUDIES
CASE 1: 
TAXONOMY + LISTINGS 
/ about 
/ about / managers 
/ about / developers 
/ events 
/ leasing-info 
/ leasing-info / businesses 
/ leasing-info / tenants 
/ directory 
/ directory / forever-21 
/ directory / house-of-blues 
/ directory / …
CASE 1: 
TAXONOMY + LISTINGS
CASE 2: 
ACADEMIC JOURNAL URLS 
/ ija 
/ ijem 
/ ijo 
/ ijo / v1 
/ ijo / v1 / i3 
/ ijo / v1 / i3 / new-frontiers-in-orthopaedics 
/ about 
/ contact
CASE 2: 
ACADEMIC JOURNAL URLS 
/ij{alpha} 
/ij{alpha}/v{integer} 
/ij{alpha}/v{integer}/i{integer} 
/ij{alpha}/v{integer}/i{integer}/ 
{url_title:alpha_dash} 
/about 
/contact
CASE 3: 
FRONT-END CP + GLOBALS 
/ cp 
• logged in: redirect me to the add-articles screen 
• logged out: show me login screen 
/ cp / add-articles 
• logged in: show login screen 
• logged out: show the login screen 
• redirect me to the add articles screen 
/ cp / edit-articles 
• logged out: show login screen 
• logged in member: show 403 
• logged in editor: show edit articles screen
CASE 3: 
FRONT-END CP + GLOBALS 
$RR['qmp/:all'] = function($router, $wildcard) { 
if ( 
in_array(ee()->session->userdata('group_id'), array(1,5,6,7,8,9)) 
) { 
if ($wildcard->value == null) { 
$router->redirect('qmp/dashboard'); 
} 
} 
else { 
if ($wildcard->value == null) { 
$router->redirect('qmp/login'); 
} 
else { 
$router->setTemplate('qmp/login'); 
} 
} 
};
CASE 4: 
MEMBER PROFILE JSON API 
• Members publish profiles on one site 
• Family sites within the organization display this data
CASE 4: 
MEMBER PROFILE JSON API 
$config['resource_router'] = array( 
'api/members' => function($router) { 
$query = ee()->db->select('title, url_title') 
->where('status', 'open') 
->where('channel_id', 1) 
->order_by('entry_date', 'ASC') 
->limit(10) 
->get('channel_titles'); 
$result = $query->result(); 
$router->json($result); 
} 
);
CASE 5: 
STAGED ROUTES 
I want my URL routes to be different based on my 
environment. 
• Multi-env config + Resource Router FTW!
RECAP 
Break out of the default URL scheme to: 
• Improve template organization 
• Deal with hierarchical or complex URL patterns 
• Decrease logic in templates 
Tools at our disposal 
• EE Template Routes 
• Structure (or Pages) 
• Resource Router 
• custom extension
QUESTIONS?
LEARN MORE 
James Smith’s ExpressionEngine URL Schematic: 
• http://www.jamessmith.co.uk/articles/expressionengine_url_schematic 
EE Template Routes docs: 
• https://ellislab.com/expressionengine/user-guide/ 
urls/template_routes.html 
EE Extension Hooks docs: 
• https://ellislab.com/expressionengine/user-guide/ 
development/extension_hooks/global/core/index.html 
Resource Router docs: 
• https://github.com/rsanchez/resource_router 
Structure docs: 
• http://buildwithstructure.com/documentation
RESOURCE 
ROUTING 
IN EE 
@michaelrog 
#EEconf 2014

More Related Content

What's hot

Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
Nate Abele
 
Let's write secure drupal code! - Drupal Camp Pannonia 2019
Let's write secure drupal code! - Drupal Camp Pannonia 2019Let's write secure drupal code! - Drupal Camp Pannonia 2019
Let's write secure drupal code! - Drupal Camp Pannonia 2019
Balázs Tatár
 
Let's write secure Drupal code! DUG Belgium - 08/08/2019
Let's write secure Drupal code! DUG Belgium - 08/08/2019Let's write secure Drupal code! DUG Belgium - 08/08/2019
Let's write secure Drupal code! DUG Belgium - 08/08/2019
Balázs Tatár
 
PHP API
PHP APIPHP API
PHP API
Jon Meredith
 
CakeFest 2013 keynote
CakeFest 2013 keynoteCakeFest 2013 keynote
CakeFest 2013 keynote
José Lorenzo Rodríguez Urdaneta
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web Framework
Ben Scofield
 
Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
José Lorenzo Rodríguez Urdaneta
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?
Alexandru Badiu
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
Marcus Ramberg
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & REST
Hugo Hamon
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
Kris Wallsmith
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2
zfconfua
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHP
markstory
 
New in cakephp3
New in cakephp3New in cakephp3
New in cakephp3
markstory
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Edition
ddiers
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl course
Marc Logghe
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Developmentjsmith92
 

What's hot (20)

Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
Let's write secure drupal code! - Drupal Camp Pannonia 2019
Let's write secure drupal code! - Drupal Camp Pannonia 2019Let's write secure drupal code! - Drupal Camp Pannonia 2019
Let's write secure drupal code! - Drupal Camp Pannonia 2019
 
Let's write secure Drupal code! DUG Belgium - 08/08/2019
Let's write secure Drupal code! DUG Belgium - 08/08/2019Let's write secure Drupal code! DUG Belgium - 08/08/2019
Let's write secure Drupal code! DUG Belgium - 08/08/2019
 
PHP API
PHP APIPHP API
PHP API
 
CakeFest 2013 keynote
CakeFest 2013 keynoteCakeFest 2013 keynote
CakeFest 2013 keynote
 
Any tutor
Any tutorAny tutor
Any tutor
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web Framework
 
Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & REST
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHP
 
New in cakephp3
New in cakephp3New in cakephp3
New in cakephp3
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Edition
 
Assetic (OSCON)
Assetic (OSCON)Assetic (OSCON)
Assetic (OSCON)
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl course
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Jersey
JerseyJersey
Jersey
 

Similar to Resource Routing in ExpressionEngine

Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Mike Schinkel
 
WordPress Café: Using WordPress as a Framework
WordPress Café: Using WordPress as a FrameworkWordPress Café: Using WordPress as a Framework
WordPress Café: Using WordPress as a Framework
Exove
 
第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 DatasourceKaz Watanabe
 
WordPress for developers - phpday 2011
WordPress for developers -  phpday 2011WordPress for developers -  phpday 2011
WordPress for developers - phpday 2011
Maurizio Pelizzone
 
Broadleaf Presents Thymeleaf
Broadleaf Presents ThymeleafBroadleaf Presents Thymeleaf
Broadleaf Presents Thymeleaf
Broadleaf Commerce
 
03 form-data
03 form-data03 form-data
03 form-datasnopteck
 
Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8
Michelangelo van Dam
 
Modularity and Layered Data Model
Modularity and Layered Data ModelModularity and Layered Data Model
Modularity and Layered Data Model
Attila Jenei
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applicationselliando dias
 
Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010
Michelangelo van Dam
 
Java colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rsJava colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rs
Sagara Gunathunga
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
Oscar Merida
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
Nate Abele
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
Hisateru Tanaka
 
Nashvile Symfony Routes Presentation
Nashvile Symfony Routes PresentationNashvile Symfony Routes Presentation
Nashvile Symfony Routes Presentation
Brent Shaffer
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHPHari K T
 
Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Fabien Potencier
 
Services Drupalcamp Stockholm 2009
Services Drupalcamp Stockholm 2009Services Drupalcamp Stockholm 2009
Services Drupalcamp Stockholm 2009
hugowetterberg
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
Pierre MARTIN
 

Similar to Resource Routing in ExpressionEngine (20)

Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
 
WordPress Café: Using WordPress as a Framework
WordPress Café: Using WordPress as a FrameworkWordPress Café: Using WordPress as a Framework
WordPress Café: Using WordPress as a Framework
 
第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource
 
WordPress for developers - phpday 2011
WordPress for developers -  phpday 2011WordPress for developers -  phpday 2011
WordPress for developers - phpday 2011
 
Sightly - Part 2
Sightly - Part 2Sightly - Part 2
Sightly - Part 2
 
Broadleaf Presents Thymeleaf
Broadleaf Presents ThymeleafBroadleaf Presents Thymeleaf
Broadleaf Presents Thymeleaf
 
03 form-data
03 form-data03 form-data
03 form-data
 
Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8
 
Modularity and Layered Data Model
Modularity and Layered Data ModelModularity and Layered Data Model
Modularity and Layered Data Model
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010
 
Java colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rsJava colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rs
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
Nashvile Symfony Routes Presentation
Nashvile Symfony Routes PresentationNashvile Symfony Routes Presentation
Nashvile Symfony Routes Presentation
 
Aura Project for PHP
Aura Project for PHPAura Project for PHP
Aura Project for PHP
 
Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
 
Services Drupalcamp Stockholm 2009
Services Drupalcamp Stockholm 2009Services Drupalcamp Stockholm 2009
Services Drupalcamp Stockholm 2009
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 

Recently uploaded

OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
Peter Caitens
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
varshanayak241
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Hivelance Technology
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 

Recently uploaded (20)

OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 

Resource Routing in ExpressionEngine

  • 1. RESOURCE ROUTING IN EE @michaelrog #EEconf 2014
  • 2. RESOURCE ROUTING FOR TRIPLE-DIGIT GROWTH!!!!!!1 @michaelrog #EEconf 2014
  • 4. WHAT IS RESOURCE ROUTING? The stage in a request where the server and/or application decides what resource to provide, and in what form, based on the URL, info in the request, and the characteristics of the server environment.
  • 5. WHAT IS RESOURCE ROUTING? The stage in a request where the server and/or application decides what resource to provide, and in what form, based on the URL, info in the request, and the characteristics of the server environment.
  • 6. WHAT IS RESOURCE ROUTING? This happens when… • Apache gives you a file • Apache gives you a file based on an .htaccess rewrite/directive • Wikipedia decides what article to give you • an API decides what controller to instantiate • EE decides what template to process
  • 7. HOW DOES EE ROUTE URLS TO TEMPLATES?
  • 8.
  • 9. WHY WOULD WE WANT TO CHANGE IT? To break out of the group/template scheme • Templates should be organized by content type, not content values • Hierarchical content • Ambiguous/conflicting segments To add pre-routing functionality • Business logic should live in the app • Templates are views Performance
  • 10. THIS IS (USUALLY) BAD: {if segment 2 == "category"} {embed="blog/.category"} {if:elseif segment_2 == "view"} {embed="blog/.single"} {if:else} {embed="blog/.listing"} {/if}
  • 11. HOW CAN WE CHANGE IT?
  • 13. NATIVE TEMPLATE ROUTES • One route per template • Can contain static or dynamic segments /products/boots /products/{product_name:alpha_dash} • Segments can contain multiple variables /{month:integer}-{day:integer}-{year:integer} /{date:regex[(d{2}-d{2}-d{4})]} • Segments can be bound by multiple rules {variable:rule1|rule2[arg0, arg1, ...]|...}
  • 14. NATIVE TEMPLATE ROUTES • Order matters /events/{type:alpha_dash} /events/{date:regex[(d{2}-d{2}-d{4})]} • Include All Segments /name/{first_name:alpha}/{last_name:alpha}/ {suffix:regex[(i|v|x)+]} /name/{first_name:alpha}/{last_name:alpha} /name/{first_name:alpha}
  • 16. SESSIONS_END $this->extensions->call('sessions_end', $this); if ($this->extensions->end_script === TRUE) { return; }
  • 17. SESSIONS_END public sessions_end($sess) { $redirect = ... ; $url = ... ; if ($redirect) { $this->EE->session = $sess; $this->EE->functions->redirect($url); } return true; }
  • 18. CORE_TEMPLATE_ROUTE HOOK $edata = ee()->extensions ->call('core_template_route’, ee()->uri->uri_string); if (is_array($edata) && count($edata) == 2) { list($template_group, $template) = $edata; }
  • 19. CORE_TEMPLATE_ROUTE HOOK public sessions_end($uri) { $template1 = ... ; $template2 = ... ; $where_to_go = (... ? $template1 : $template2); return array( 'template_group', // Template group name 'template' // Template name ); }
  • 20. ADD-ONS THAT DO THIS • Structure — http://buildwithstructure.com/ • Overrides front-end routes based on hierarchical URIs • Overrides CP routes based on optional Settings • Resource Router — https://github.com/rsanchez/resource_router • Remap URI routes to an HTTP response, using CodeIgniter-style routings
  • 21. STRUCTURE Use case: • Entries as pages in a hierarchy or taxonomy.
  • 22. STRUCTURE Each Channel has a default template for its entries, and a default behavior: Page or Listing
  • 24. STRUCTURE Structure even hijacks CP routes a bit: I can make Structure my CP homepage, and redirect to it after an entry is saved.
  • 25. RESOURCE ROUTER Use cases: • to break out of the template_group/template_name paradigm of traditional EE routing • to nest URLs on a Structure/Pages URI (pagination, categories, etc.) • to point multiple URL patterns to a single template • to add custom JSON/HTML endpoints that do not warrant a full EE template • to remove excess conditional logic in your templates
  • 26. RESOURCE ROUTER $config['resource_router'] = array( 'uri/pattern' => ... )
  • 27. RESOURCE ROUTER {if segment 2 == "category"} {embed="blog/.category"} {if:elseif segment_2 == "view"} {embed="blog/.single"} {if:else} {embed="blog/.listing"} {/if}
  • 28. RESOURCE ROUTER $config['resource_router'] = array( 'blog/:any' => function ($router, $wildcard) { if ($wildcard->value == 'category') { $router->setTemplate('blog/.category'); } elseif ($wildcard->value === 'view') { $router->setTemplate('blog/.single'); } else { $router->setTemplate('blog/.listing'); } } )
  • 29. RESOURCE ROUTER Wildcards • :any • :num • :year • :month • :day • :pagination • :category • :page:XX • :all
  • 30. RESOURCE ROUTER $config['resource_router'] = array( 'blog/:category' => 'site/blog-category', 'blog/:year' => 'site/blog-yearly-archive', );
  • 31. RESOURCE ROUTER Validating Wildcards • :entry_id • :url_title • :category_id • :category_url_title • :member_id • :username
  • 32. RESOURCE ROUTER Callbacks • $router->setTemplate(string $template) • $router->set404() • $router->setGlobal(string $key, $value) • $router->setVariable(string $key, mixed $value) • {exp:resource_router:your_var_name} • $router->setHeader(string $name, string $value) • $router->json(mixed $data) • $router->redirect(string $url, int $statusCode)
  • 33. RESOURCE ROUTER Wildcard functions • $wildcard->isValidEntryId(array $where) • $wildcard->isValidUrlTitle(array $where) • $wildcard->isValidEntry(array $where) • $wildcard->isValidCategoryId(array $where) • $wildcard->isValidCategoryUrlTitle(array $where) • $wildcard->isValidCategory(array $where) • $wildcard->isValidMemberId(array $where) • $wildcard->isValidUsername(array $where) • $wildcard->getMeta(string $column)
  • 35. CASE 1: TAXONOMY + LISTINGS / about / about / managers / about / developers / events / leasing-info / leasing-info / businesses / leasing-info / tenants / directory / directory / forever-21 / directory / house-of-blues / directory / …
  • 36. CASE 1: TAXONOMY + LISTINGS
  • 37. CASE 2: ACADEMIC JOURNAL URLS / ija / ijem / ijo / ijo / v1 / ijo / v1 / i3 / ijo / v1 / i3 / new-frontiers-in-orthopaedics / about / contact
  • 38. CASE 2: ACADEMIC JOURNAL URLS /ij{alpha} /ij{alpha}/v{integer} /ij{alpha}/v{integer}/i{integer} /ij{alpha}/v{integer}/i{integer}/ {url_title:alpha_dash} /about /contact
  • 39. CASE 3: FRONT-END CP + GLOBALS / cp • logged in: redirect me to the add-articles screen • logged out: show me login screen / cp / add-articles • logged in: show login screen • logged out: show the login screen • redirect me to the add articles screen / cp / edit-articles • logged out: show login screen • logged in member: show 403 • logged in editor: show edit articles screen
  • 40. CASE 3: FRONT-END CP + GLOBALS $RR['qmp/:all'] = function($router, $wildcard) { if ( in_array(ee()->session->userdata('group_id'), array(1,5,6,7,8,9)) ) { if ($wildcard->value == null) { $router->redirect('qmp/dashboard'); } } else { if ($wildcard->value == null) { $router->redirect('qmp/login'); } else { $router->setTemplate('qmp/login'); } } };
  • 41. CASE 4: MEMBER PROFILE JSON API • Members publish profiles on one site • Family sites within the organization display this data
  • 42. CASE 4: MEMBER PROFILE JSON API $config['resource_router'] = array( 'api/members' => function($router) { $query = ee()->db->select('title, url_title') ->where('status', 'open') ->where('channel_id', 1) ->order_by('entry_date', 'ASC') ->limit(10) ->get('channel_titles'); $result = $query->result(); $router->json($result); } );
  • 43. CASE 5: STAGED ROUTES I want my URL routes to be different based on my environment. • Multi-env config + Resource Router FTW!
  • 44. RECAP Break out of the default URL scheme to: • Improve template organization • Deal with hierarchical or complex URL patterns • Decrease logic in templates Tools at our disposal • EE Template Routes • Structure (or Pages) • Resource Router • custom extension
  • 46. LEARN MORE James Smith’s ExpressionEngine URL Schematic: • http://www.jamessmith.co.uk/articles/expressionengine_url_schematic EE Template Routes docs: • https://ellislab.com/expressionengine/user-guide/ urls/template_routes.html EE Extension Hooks docs: • https://ellislab.com/expressionengine/user-guide/ development/extension_hooks/global/core/index.html Resource Router docs: • https://github.com/rsanchez/resource_router Structure docs: • http://buildwithstructure.com/documentation
  • 47. RESOURCE ROUTING IN EE @michaelrog #EEconf 2014