How to Use TYPO3 Context to Improve Environment?

Maximize your TYPO3 site's potential with TYPO3 Context! Our guide offers expert tips and best practices to streamline development and Performance.

How to Use TYPO3 Context to Improve Environment?

Do you know what’s TYPO3 Context? Or, Are you looking for a beginning to advanced-level configuration guide to manage Web-server & Application-level TYPO3 Context? In this article, you will learn a step-by-step practical guide to use TYPO3 Context eg., Database configuration, TypoScript condition, Site configuration, TYPO3-CLI, etc.

Modern TYPO3 developers are working on automation and multiple TYPO3 environments like development server, production server, staging environment, etc. Fortunately, for years, TYPO3 CMS provides developer-friendly TYPO3 Context configuration out-of-box.

Let’s get ready to get skills to configure TYPO3 context at the server level as well as the application context level.

In TYPO3 CMS, You can easily get know - What's the current TYPO3 Application Context from the top panel (above screenshot).

In simple terms, the TYPO3 context is an approach to define a particular TYPO3 environment like production, staging, a testing server, etc.. It means, for your every environment, you can define a particular configuration with available conditions in PHP, Extbase, TypoScript, Yaml, etc.

Here are examples of different possible types of TYPO3 Context.

Each request, no matter if it runs from the command line or through HTTP, runs in a specific application context. TYPO3 CMS provides exactly three built-in contexts:

The TYPO3_CONTEXT Variable

Dedicated environment variable which tells TYPO3 to define different context environments.

Which are default available TYPO3 Context?

  • Production (default) - should be used for a live site
  • Development - used for development
  • Testing - is only used internally when executing TYPO3 core tests. It must not be used otherwise.

Is Any Custom TYPO3 Context? Yes, Sub-Context!

Of course, You can create your own TYPO3 context by defining sub-text, Here are some examples.

Syntax

  • <MainContext>/<SubContext>

Examples

  • Production/Live
  • Production/Live/Server2
  • Development/Staging

Testing

Production

1st step to creating a TYPO3 context, You will need to define “TYPO3_CONTEXT” at your favorite web-server, Let’s see how!

What are the ways to define TYPO3 Context in the Apache webserver?

You can configure the TYPO3 context with different ways in the Apache web server as below.

Option #1 Using Apache’s SetEnv Directive
Contact your server administrator to create SetEnv Apache’s configuration, Here are some examples.

 

# Define main TYPO3 context
SetEnv TYPO3_CONTEXT Development

# Define domain-wise main TYPO3 context
SetEnvIf Host www\.t3terminal\.com$ TYPO3_CONTEXT=Production
SetEnvIf Host testing\.t3terminal\.com$ TYPO3_CONTEXT=Testing
SetEnvIf Host dev\.t3terminal\.local$ TYPO3_CONTEXT=Development

 

Option #2 Using .Htaccess or mod_rewrite
If you are using the Apache server with shared hosting, you can also define TYPO3_CONTEXT with .htaccess or mode_rewrite vhost.

 

# In your Apache configuration (either .htaccess or vhost)
# you can either set a context to static value with:
SetEnv TYPO3_CONTEXT Development

# Or set context depending on the current host header
# using mod_rewrite module

RewriteCond %{HTTP_HOST} ^dev\.t3terminal\.com$
RewriteRule .? - [E=TYPO3_CONTEXT:Development]

RewriteCond %{HTTP_HOST} ^staging\.t3terminal\.com$
RewriteRule .? - [E=TYPO3_CONTEXT:Production/Staging]

RewriteCond %{HTTP_HOST} ^www\.t3terminal\.com$
RewriteRule .? - [E=TYPO3_CONTEXT:Production]

# or using setenvif module
SetEnvIf Host "^dev\.t3terminal\.com$" TYPO3_CONTEXT=Development
SetEnvIf Host "^staging\.t3terminal\.com$" TYPO3_CONTEXT=Production/Staging
SetEnvIf Host "^www\.t3terminal\.com$" TYPO3_CONTEXT=Production

 

Option #3 TYPO3_Context without .Htaccess
If somehow you don’t have access to modify with .htaccess or mode_rewrite, you can try this solution TYPO3 Context without .Htaccess (Note: Not tested by me yet).

How can we configure TYPO3 Context in NGNIX?

Many people have started to work with NGNIX which is really great, You can easily configure NGNIX configuration as below.

 

location ~ \.php$ {
  include         fastcgi_params;
 fastcgi_index   index.php;
 fastcgi_param   TYPO3_CONTEXT  Development/Dev;
  fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}

 

How to set up the TYPO3 Context in Docker + DDEV?

TYPO3 community loves DDEV solution as a local development environment, we love too :) You can simply configure TYPO3_CONTEXT in Docker/DDEV with the below steps.

Step 1. Create docker-compose context file into your .ddev folder

 

touch .ddev/docker-compose.context.yaml

 

Step 2. Add the below configuration to the file

 

version: '3.6'
services:
   web:
    environment:
- TYPO3_CONTEXT=Development

 

Step 3. Restart your DDEV

 

ddev restart

Can we configure the TYPO3 Context in CLI-based?

Yes, why not! Nowadays, TYPO3 developers are getting smarter and started to work with TYPO3-CLI like a popular solution of “typo3-console package”. To configure the TYPO3 context in CLI, Follow these steps.

Step 1. Go to the home directory (Linux or Mac)

Step 2. Find .profile or .bashrc

Step 3. Configure TYPO3_CONTEXT

 

export TYPO3_CONTEXT=Testing

 

Step 4. Cross-verify it

 

env | grep TYPO3_CONTEXT

By d way, You can specify “TYPO3_CONTEXT” for every command at the run time of your TYPO3 CLI-commands as below.

 

# Run in Development TYPO3 Context
TYPO3_CONTEXT=Development ./typo3/cli_dispatch.phpsh

# Run in Production TYPO3 Context
TYPO3_CONTEXT=Production exec ./vendor/bin/typo3cms cache:flush

# Run in Testing TYPO3 Context
TYPO3_CONTEXT=Testing exec ./vendor/bin/typo3cms install:fixfolderstructure

 

 

Well done! I hope you have successfully configured the TYPO3_CONTEXT variable to your web-server, local development environment, and/or TYPO3-CLI.

Now, Are you excited about how easy it is to use TYPO3_CONTEXT in real-life TYPO3 applications features, some examples likes;

  • Each TYPO3 environment should have a separate TYPO3 site URL (through config.yaml).
  • Each TYPO3 environment should have a separate Database configuration (through AdditionalConfiguration.php).
  • Each TYPO3 environment may have different templating execution (through TypoScript)
  • And so on, based on your TYPO3 application needs you can use TYPO3_CONTEX

Since TYPO3 v9, We have a cool site management module to manage multilingual, site-URLs, etc. To create a separate TYPO3 site-URL of your each TYPO3 context environment/server, You can configure the below settings.

 

# config/sites/project/config.yaml (composer-based TYPO3 instance)
# or
# typo3conf/ext/sites/project/config.yaml (non-composer based TYPO3 instance)
rootPageId: 1
base: 'https://t3planet.com/'
baseVariants:
  -
    base: 'http://dev.t3planet.com/'
    condition: 'applicationContext == "Development"'
  -
    base: 'http://staging.t3planet.com/'
    condition: 'applicationContext == "Production/Staging"'

One of the most important to use TYPO3_CONTEXT is to define multiple database configurations for each of your TYPO3 environments.

TYPO3 provides developer-friendly features to get the current TYPO3 application context.

 

// Since TYPO3 9LTS
\TYPO3\CMS\Core\Core\Environment::getContext()

// Prior to TYPO3 9LTS
\TYPO3\CMS\Core\Utility\GeneralUtility::getApplicationContext()

 

You can easily achieve it by adding PHP-code to your AdditionalConfiguration.php, Here is the sample.

 

// typo3conf/AdditionalConfiguration.php
<?php
switch (\TYPO3\CMS\Core\Core\Environment::getContext()) {
    case 'Development':
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['displayErrors'] = 1;
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] = '*';

        $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default'] = array_merge(
            isset($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default'])
            ? $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']
            : [],
            [
                'dbname' => 'db',
                'host' => 'db',
                'password' => 'db',
                'port' => '3306',
                'user' => 'db',
            ]
        );
        break;

    case 'Production':
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['displayErrors'] = 0;
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] = '192.168.1.*';

        $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default'] = array_merge(
            isset($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default'])
            ? $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']
            : [],
            [
                'dbname' => 'project',
                'host' => '127.0.0.1',
                'password' => 'password',
                'port' => '3306',
                'user' => 'user',
            ]
        );
        break;
}

Use 3. PHP TYPO3 Core APIs (for Extbase/Fluid)

Do you want to use the TYPO3 context to your custom TYPO3 extension or template? Then, you can easily call TYPO3 core context APIs as below.
 

 

$applicationContext = \TYPO3\CMS\Core\Utility\GeneralUtility::getApplicationContext();
$applicationContext->isDevelopment();
$applicationContext->isTesting();
$applicationContext->isProduction();

 

 

And the last one, Based on your needs uses “applicationContext” Symfony expression TypoScript to check the TYPO3 context environment.

There can be a number of practical examples eg., At production server, you can enable compress javascript files, while development should have disabled such compression ;)

 

# Check in Production/Live
[applicationContext = Production/Live]
   config.contentObjectExceptionHandler = 0
   config {
      compressJS = 1
      concatenateJS = 1
   }
[end]

# Check in Development mode
[applicationContext = Development]
   config.contentObjectExceptionHandler = 1
   config {
      compressJS = 0
      concatenateJS = 0
   }
[end]

# Check in more then one TYPO3 context by “,” comma separated
[applicationContext = Development/Debugging, Development/ClientA]
  [...]
[end]

# Check with regular expression to match your TYPO3 context
[applicationContext = /^Development\/Preview\/d+$/]
   [...]
[end]

Using the TYPO3 command `typo3/sysext/core/bin/typo3`, You can now know the current TYPO3 Application Context.

I’ve found one TYPO3 extension which is related to TYPO3_CONTEXT. By d way, I’ve not tested this extension. If you know it well, share your experience in the comment box.

TYPO3 context configuration loader (EXT:context_loader)

A simple TYPO3 configuration loader to create and configure your TYPO3 context environment into /typo3conf/AdditionalConfiguration/

Thanks for reading the article.

I’m doing my best to keep a simple and easy guide blog for you, I hope you learn with fun :) Let’s quickly recap the article.

  • Understand and figure out why and how you require the TYPO3 Context feature? You should prepare where you should implement the TYPO3 context.
  • Make sure, Your web server (like Apache, NGINX) is ready and configured with the TYPO3 context.
  • Start to implement application-level TYPO3 context with PHP, TypoScript, YAML eg., Multiple database configuration, Site-URLs, etc.

Do you have any experience with the TYPO3 context? Or, Any questions to implement the TYPO3 context? I would love to receive your experience to keep improving such small but cool TYPO3 features.

Have a Happy TYPO3 Contexting!

Your One-Stop Solutions for Custom TYPO3 Development

  • A Decade of TYPO3 Industry Experience
  • 350+ Successful TYPO3 Projects
  • 87% Repeat TYPO3 Customers
TYPO3 Service
service

Post a Comment

×
Captcha Code Can't read the image? Click here to refresh
  • user
    Peter Schmidt 2023-07-14 at 3:22 pm
    I never realized the power of TYPO3 Context until I read your blog post. The insights you provided on how to leverage it to improve the environment were eye-opening.
  • user
    Ottfried Galen 2020-10-06 at 8:35 am
    Thank you for this serious great info on TYPO3 context and TYPO3 environments. It's always pleasure reading your insights Sanjay.
  • user
    Franz Holzinger 2020-10-05 at 2:39 pm
    There are 2 more errors in the config.yaml example.
    The closing quote is missing at the end.

    -
    base: 'http://dev.t3planet.com/'
    condition: 'applicationContext == "Development"
    -
    base: 'http://staging.t3planet.com/'
    condition: 'applicationContext == "Production/Staging"

    ===>

    -
    base: 'http://dev.t3planet.com/'
    condition: 'applicationContext == "Development"'
    -
    base: 'http://staging.t3planet.com/'
    condition: 'applicationContext == "Production/Staging"'