Search code examples
phpjoomlavirtuemart

joomla Jfactory does not instantiate itselft in a joomla webapp ( joomla 2.5 )


I am trying to building an external webApp to upload some products in Joomla! with virtuemart.
All works as expected but I have still a problem with Jfactory::getApplication.

I have followed this tutorial:

http://docs.joomla.org/How_to_create_a_stand-alone_application_using_the_Joomla!_Platform

But when I go calling the VmModel on the __construct method it calls a parent::__construct($config):

$this->_cidName = $cidName;
// Get the pagination request variables
$mainframe = JFactory::getApplication() ;
$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');

That raises the following error:

vCall to undefined method JException::getUserStateFromRequest()

If could help there is also:

vNotice: Trying to get property of non-object in /home/giuseppe/homeProj/ModaNuovo/libraries/joomla/application/application.php on line 201

raised by:

public static function getInstance($client, $config = array(), $prefix = 'J')
{
    if (empty(self::$instances[$client]))
    {
        // Load the router object.
        $info = JApplicationHelper::getClientInfo($client, true);

        $path = $info->path . '/includes/application.php';
        if (file_exists($path))

Solution

  • The solution is to instantiate the main app together with your one, and if needed also login the following way:

    $app = JFactory::getApplication('administrator');
    
    // create a form token
    $user = JFactory::getUser(42);
    $session = JFactory::getSession();
    $hash = JApplication::getHash($user->get('id', 0) . $session->getToken(true));
    

    If needed you have also to perform login to validate this hash.