tl;dr: Using the benefits from using composer class loading to stabilize TYPO3 6.2 projects
The benefits we saw when implementing composer class loading into TYPO3 7LTS were so tremendous, that we took the additional effort to backport some of these changes to TYPO3 6.2.10. By default TYPO3 since 6.2.10 uses the composer class loader to load all classes for required TYPO3 core extensions, but you can use it to load every class in your TYPO3 6.2 project. To do so, you have a little extra effort, but you can say goodby to any class loading related troubles during deployment or after clearing caches.
Introduction
What I'm going to describe here is not a complete composer setup, which would also be perfectly possible with TYPO3 6.2, but the few steps you need to take to take advantage from composer class loading in your existing non composer TYPO3 6.2. projects. If you already use composer to setup your TYPO3 projects, you may as well want to have a peak into this article to make sure you get the most out of the possibilities.
Setup
A prerequisite for this to work is, that you put your TYPO3 sources in a folder, not a link, and this folder resides on the same level as your index.php like this (I skipped fileadmin, typo3conf, … directories here for better overview):
Add this composer.json file on the same directory level:
Step: 2: Adapt class loading registration in composer.json
The example composer.json file from the gist comes with class loading information for every TYPO3 core extension, including the class aliases for 4.x compatiblity. What you now need to do is add class loading information for every single extension in your installation. I prepared examples for typo3_console, news and tt_news, which should be self-explanatory. You basically need to add all paths in which the extensions in your installation have classes. Make sure not to forget to specify the relative path to the directory (seen from the composer.json file).
You can even try to only specify "classmap": ["typo3conf/ext"] as a single path, which saves you quite some time and works out in most cases, but can fail in some edge cases.
Step 3: Perform a composer install in this directory
Download composer if not done already, navigate to the directory with the composer.json file and perform a composer install. The result should look like this:
It is mandatory that the Packages folder, which is created during the composer install run is on the same level as the typo3_src folder.
Step 4: Set the TYPO3_COMPOSER_AUTOLOAD environment variable
Add the following to your apache vhost configuration:
SetEnv TYPO3_COMPOSER_AUTOLOAD "On"
or the following in your .htaccess file:
RewriteRule .? - [E= TYPO3_COMPOSER_AUTOLOAD:On]
For nginx, you can set a fcgi param like that:
fastcgi_param TYPO3_COMPOSER_AUTOLOAD On;
Verify that it worked
Remove all class loader cache files in typo3temp/Cache/Data/cache_classes and load the backend and frontend of your website. The website should show up and no cache files should be added to this directory. Do not delete the complete typo3temp/Cache because then, a few class cache entries are written anyway and you cannot confirm or deny whether you did everything correctly.
Optimize
If you do not have every single TYPO3 core extension installed, you should consider removing the lines from the composer.json file which point to core extensions you do not use in the project.
If you do not want to expose the TYPO3 sources directory and the Packages directory and your composer.json file, as they contain quite some information about your installation, you can create a web folder and link the necessary folders and files into it, e.g. like that:
$ ls -l web/ lrwxr-xr-x 1 12 Oct 10 17:43 fileadmin -> ../fileadmin lrwxr-xr-x 1 12 Oct 10 17:44 index.php -> ../index.php lrwxr-xr-x 1 8 Oct 10 17:44 typo3 -> ../typo3 lrwxr-xr-x 1 12 Oct 10 17:45 typo3conf -> ../typo3conf lrwxr-xr-x 1 12 Oct 10 17:44 typo3temp -> ../typo3temp lrwxr-xr-x 1 10 Oct 10 17:44 uploads -> ../uploads
Last but not least: Enjoy the gained stability and speed!
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
✓ Live Streaming✓ Interactive Chat✓ Private Shows✓ HD Quality
Anya is LIVE right now
FREE
Free to watch • No registration required • HD streaming
How to configure class loading for extensions in TYPO3 7LTS
tl;dr: In 99,9% of the cases, you do not have to do anything. Every configuration option below is just for clarity and convenience during development or to cover the 0,1% edge case. It is however considered best practice to add any of the following configuration options.
In my last article I gave a lot of background information why we discarded our class loader in TYPO3 7LTS and switched to use the one provided by composer. This post is more a tutorial with a lot more examples. It has a focus on how rather than why and the target group is TYPO3 extension developers.
Minimum setup
In this section I describe what you can do to get better class loading suport in TYPO3 7LTS. Everthing you find here, you can safely do without loosing compatibility with older TYPO3 versions!
Compatiblity mode
If you don't want to, you do not have to do anything! Regarding class loading your extension will work with TYPO3 7LTS anyway in 99,9% of the cases. TYPO3 scans the extension directory for class or interface files and registers every single one automatically to ensure backwards compatibility.
Important: If you add classes during development they will not be found by the class loader unless you update your class information files. If you have classes located e.g. in a tests folder which should not be availble in production, you should consider one of the following ways to configure class loading.
Extensions having one folder with classes
Considering you have an Extbase extension (or an extension where all classes and interfaces reside in a Classes folder) you can simply add the following to your ext_emconf.php file:
The folder Classes will be scanned and every class or interface found will automatically be registered, no matter if the classes use PHP namspaces or not.
Important: If you add classes during development they will not be found by the class loader unless you update your class information
Extensions using namespaces (possible since TYPO3 6.0)
TYPO3 introduced a way to load classes using namespaces, which was later standardized by the PHP framework interoperability group as PSR-4. If your extension has namespaced classes, then you can add the following to your ext_emconf.php file:
Here you do not only specify a folder, but also a namespace prefix which all classes in this folder must have. The prefix must end with a backslash!
Note: If you do so, newly added classes during development can be resolved automatically, whithout any further action! There is no need to update your class information files!
Legacy extension which previously used an ext_autoload.php file
As outlined before, it is not necessary any more to specify the name and location of every single class. You can specify folders and TYPO3 scans for the classes in them. So for example to provide class information for tt_news, Rupi could add the following to the ext_emconf.php:
Note: As you can see, it is perfectly fine to add multiple folders or even single files to the class map. However, just to stress it again, if you add classes during development they will not be found by the class loader unless you update your class information files manually.
Advanced setup
This section is a collection of further things you can do to tweak your development experience regarding class loading in TYPO3 7LTS.
Class loading information in composer.json
If your extension has a composer.json file already with a psr-4 or classmap autoload section, you are done. No further action is required. The section in the composer.json file can look like this:
As you can see, you can combine both classmap and psr-4 sections (which is also possible of course in an ext_emconf.php file.
Fixture classes
The old TYPO3 class loader could automatically resolve classes inside a Tests folder, in case the namespace reflected the folder structure below. In TYPO3 7LTS this is not the case any more, which really is a good thing! If you do not want to use composer to execute your extension tests individually like I described in a previous article, but execute them in the context of your TYPO3 project installation, you can add an autoload-dev section in your ext_emconf.php file:
Note: Just like composer, TYPO3 ignores an autoload-dev section in a composer.json file. So unlike the regular autoload section, the autoload-dev section is only evaluated if present in the ext_emconf.php file.
General notes
Update class loading information
The class loading information is updated every time you activate or deactivate an extension via extension manager. In the rare case you want to update it manually, you can run the following command from the command line:
It is possible to manage the source code for your TYPO3 project exclusively with composer (which I can highly recommend). In that case, all class loading information provided by extensions in the ext_emconf.php files is completely ignored! However as best practice and for the time being it is recommended to nevertheless provide this information in your extensions, so that it can be used in projects which are not set up using composer.
Enjoy developing with TYPO3 7LTS. It is an awesome release!