[FIXED] The entrypoint (public/index.php) in Symfony 6 – how it works?

Issue

In other words, the Symfony 6 framework (public/index.php) entry point file contains a structure that I do not understand.

Below is the entire contents of the file:

 

What I don't understand is how to use anonymous functions. Since you're at the top level, you don't know how it's executed and where the results are returned.

Solution

What happens is simple.

As you can see you are not asking for a standard autoload of composer

require_once dirname(__DIR__).'/vendor/autoload.php';

But you are requiring

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

This file is generated by symfony/runtime

The operation is simple, I'll give you an example

File index.php (entry point):

<?php
require_once __DIR__.'/runtime.php';

return function (array $context){
    print_r($context);
    return 1;
};

File runtime.php (runtime):

<?php
$app = require $_SERVER['SCRIPT_FILENAME'];

$app = $app(['context_variable'=>1]);

What Happnes:

  1. When the index is called it requires the runtime
  2. The runtime takes the variable 'SCRIPT_FILENAME' which is populated with the path of the index
  3. The autoload file then makes a require_once of the executed file and passes it some parameters
  4. Since in the index the runtime is called via require_once this will prevent files from calling each other recursively

I hope I have been exhaustive, otherwise please ask me.

Answered By - Gianfrancesco Aurecchia

Answer Checked By - David Goodson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published