Grav Lifecycle
It is often useful to know how Grav processes in order to fully understand how best to extend Grav via plugins. This is the Grav lifecycle:
index.php
- Check PHP version to ensure we're running at least version 7.1.3
- Class loader initialization
- Obtain Grav instance
Grav.php
- No instance exists, so call
load() - Add
loader - Add and initialize
debugger - Add
grav(deprecated) - Register Default Services
- Register Service Providers
- Accounts Service Provider
- Add
permissions(1.7) - Add
accounts(1.6) - Add
user_groups(1.7) - Add
users(deprecated)
- Add
- Assets Service Provider
- Add
assets
- Add
- Backups Service Provider
- Add
backups(1.6)
- Add
- Config Service Provider
- Add
setup - Add
blueprints - Add
config - Add
languages - Add
language
- Add
- Error Service Provider
- Add
error
- Add
- Filesystem Service Provider
- Add
filesystem
- Add
- Flex Service Provider
- Add
flex(1.7)
- Add
- Inflector Service Provider
- Add
inflector
- Add
- Logger Service Provider
- Add
log
- Add
- Output Service Provider
- Add
output
- Add
- Pages Service Provider
- Add
pages - Add
page
- Add
- Request Service Provider
- Add
request(1.7)
- Add
- Scheduler Service Provider
- Add
scheduler(1.6)
- Add
- Session Service Provider
- Add
session - Add
messages
- Add
- Streams Service Provider
- Add
locator - Add
streams
- Add
- Task Service Provider
- Add
task - Add
action
- Add
- Simple Service Providers
- Add
browser - Add
cache - Add
events - Add
exif - Add
plugins - Add
taxonomy - Add
themes - Add
twig - Add
uri
- Add
- Accounts Service Provider
- No instance exists, so call
- call
Grav::process()Grav.php
- Run Initialize Processor
- Configuration
- Initialize
$grav['config'] - Initialize
$grav['plugins']
- Initialize
- Logger
- Initialize
$grav['log']
- Initialize
- Errors
- Initialize
$grav['errors'] - Registers PHP error handlers
- Initialize
- Debugger
- Initialize
$grav['debugger']
- Initialize
- Handle debugger requests
- Start output buffering
- Localization
- Set the locale and timezone
- Plugins
- Initialize
$grav['plugins']
- Initialize
- Pages
- Initialize
$grav['pages']
- Initialize
- Uri
- Initialize
$grav['uri'] - Add
$grav['base_url_absolute'] - Add
$grav['base_url_relative'] - Add
$grav['base_url']
- Initialize
- Handle redirect
- Redirect if
system.pages.redirect_trailing_slashis true and trailing slash in URL
- Redirect if
- Accounts
- Initialize
$grav['accounts']
- Initialize
- Session
- Initialize
$grav['session']if Ifsystem.session.initializeistrue
- Initialize
- Configuration
- Run Plugins Processor
- Fire onPluginsInitialized event
- Run Themes Processor
- Initialize
$grav['themes'] - Fire onThemeInitialized event
- Initialize
- Run Request Processor
- Initialize
$grav['request'] - Fire onRequestHandlerInit event with [request, handler]
- If response is set inside the event, stop further processing and output the response
- Initialize
- Run Tasks Processor
- If request has attribute controller.class and either task or action:
- Run the controller
- If
NotFoundException: continue (check task and action) - If response code 418: continue (ignore task and action)
- Else: stop further processing and output the response
- If task:
- Fire onTask event
- Fire onTask.[TASK] event
- If action:
- Fire onAction event
- Fire onAction.[ACTION] event
- If request has attribute controller.class and either task or action:
- Run Backups Processor
- Initialize
$grav['backups'] - Fire onBackupsInitialized event
- Initialize
- Run Scheduler Processor
- Initialize
$grav['scheduler'] - Fire onSchedulerInitialized event
- Initialize
- Run Assets Processor
- Initialize
$grav['assets'] - Fire onAssetsInitialized event
- Initialize
- Run Twig Processor
- Initialize
$grav['twig']Twig.php
- Set Twig template paths based on configuration
- Handle language templates if available
- Fire onTwigTemplatePaths event
- Fire onTwigLoader event
- Load Twig configuration and loader chain
- Fire onTwigInitialized event
- Load Twig extensions
- Fire onTwigExtensions event
- Set standard Twig variables (config, uri, taxonomy, assets, browser, etc)
- Initialize
- Run Pages Processor
- Initialize
$grav['pages']Pages.php
- Call
buildPages() - (logic differs somewhat for Flex Pages, but the idea is the same)
- Check if cache is good
- If cache is good load pages date from
- If cache is not good call
recurse() - Fire onBuildPagesInitialized event in
recurse() - If a
.mdfile is found: - Fire onPageProcessed event
- If a
folderfoundrecurse()the children - Fire onFolderProcessed event
- Call
buildRoutes() - Initialize
taxonomyfor all pages - Build
routetable for fast lookup
- Call
- Fire onPagesInitialized event with [pages]
- Fire onPageInitialized event with [page]
- If page is not routable:
- Fire onPageNotFound event with [page]
- If task:
- Fire onPageTask event with [task, page]
- Fire onPageTask.[TASK] event with [task, page]
- If action:
- Fire onPageAction event with [action, page]
- Fire onPageAction.[ACTION] event with [action, page]
- Initialize
- Run Debugger Assets Processor
- Debugbar only: Add the debugger CSS/JS to the assets
- Run Render Processor
- Initialize
$grav['output'] - If
outputinstanceofResponseInterface:- Stop further processing and output the response
- Else:
- Render page with Twig's
processSite()methodTwig.php
- Fire onTwigSiteVariables event
- Get the page output
- Fire onTwigPageVariables, also called for each modular subpage
- If a page is not found or not routable, first fire the onPageFallBackUrl event to see if we have a fallback for a media asset and then fire onPageNotFound if not
- Set all Twig variables on the Twig object
- Set the template name based on file/header/extension information
- Call
render()method - Return resulting HTML
- Fire onOutputGenerated event
- Echo the output into output buffer
- Fire onOutputRendered event
- Build Response object
- Stop further processing and output the response
- Render page with Twig's
- Initialize
- Fire onPageHeaders event to allow page header manipulation
- Output HTTP header and body
- Render debugger (if enabled)
- Shutdown
- Close session
- Close connection to client
- Fire onShutdown event
- Run Initialize Processor
Whenever a page has its content() method called, the following lifecycle occurs: