Skip to content

Instantly share code, notes, and snippets.

@PhilETaylor
Created June 21, 2021 14:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PhilETaylor/0c36d87da0f2ad231b378b47c54cfb9b to your computer and use it in GitHub Desktop.
Save PhilETaylor/0c36d87da0f2ad231b378b47c54cfb9b to your computer and use it in GitHub Desktop.
<?php
define('_JEXEC', 1);
define('JPATH_BASE', __DIR__);
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';
// Boot the DI container
$container = \Joomla\CMS\Factory::getContainer();
/*
* Alias the session service keys to the web session service as that is the primary session backend for this application
*
* In addition to aliasing "common" service keys, we also create aliases for the PHP classes to ensure autowiring objects
* is supported. This includes aliases for aliased class names, and the keys for aliased class names should be considered
* deprecated to be removed when the class name alias is removed as well.
*/
$container->alias('session.web', 'session.web.site')
->alias('session', 'session.web.site')
->alias('JSession', 'session.web.site')
->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');
// Instantiate the application.
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
// Set the application as global app
\Joomla\CMS\Factory::$application = $app;
var_dump($app);
@thednp
Copy link

thednp commented Sep 12, 2021

For some reason this isn't working properly in my custom field class file where I'm trying to require a file from inside my template.

// the one $app you define above
$template = $app->getTemplate();

// should be myTemplateName
require_once JPATH_BASE .'/templates/'. $template . '/src/Helper/MyHelper.php';

Error:

InvalidArgumentException
Could not find template "myTemplateName".

Is this the only way to do Factory::getApplication('site') now?

Joomla 4.0.2

@PhilETaylor
Copy link
Author

Something recently screwed it up - the getTemplate method is screwed in Joomla when using it like this, I have not had time to investigate yet.

@thednp
Copy link

thednp commented Sep 12, 2021

Does this have to do with the fact that my template is installed via Extension Discovery?

Other bad things happen with this type of installation, for instance some language strings are not translated for some plugins, but IDK, I hope this gets fixed asap.

Thanks

@PhilETaylor
Copy link
Author

No, it happens without any changes to Joomla core, when using AdministratorApplication (instead of site) it fails on trying to find 'isis'

I'll take a look later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment