0

I use Joomla 2.5 for a website, but the home page is an external php page, where I need to get data from specific joomla articles and menu items.

  1. What is the best way to go, calling modules or connecting to database to retrieve what I need?
  2. How can I call the Joomla classes, functions etc to get my results?

What I've already tried:

The "module" solution from this post (Joomla Menu In External Page), which shows error concerning the session being already started.

Some code for data retrieval:

define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__).'/../../Joomla_2.5.9' );   // should point to joomla root
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('introtext')
 ->from('#__content')
 ->where('id = 1');
$db->setQuery($query);

$fullArticle = $db->loadResult();

echo $fullArticle;

This piece of code works great for getting the Article Text from a specific Article. Of course it's not that functional since I want to work around categories and multilingual content.

I will add whatever turns out to solve the problem in a better way. Any further ideas would certainly help!

Community
  • 1
  • 1
nkou
  • 36
  • 1
  • 4
  • 1
    Why is your homepage external? If you need to display on it articles, modules etc, why can't you create it inside of Joomla? – Marko D Apr 05 '13 at 12:59
  • I use a parallax home page where I need to load certain articles eg. company. It would be really difficult to accomplice something like that inside joomla. – nkou Apr 05 '13 at 13:13
  • Well, I found something that cover the data retrieval in another question. For those interested, you can check it out here: http://stackoverflow.com/questions/15042262/access-joomla-2-5-from-external-script-to-get-article-by-id – nkou Apr 05 '13 at 13:15

2 Answers2

2

You need to initialize the Joomla application:

$joomlaPath = dirname(__FILE__).'/../../Joomla_2.5.9';
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists($joomlaPath . '/defines.php')) {
    include_once $joomlaPath . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', $joomlaPath);
    require_once JPATH_BASE.'/includes/defines.php';
}

require_once JPATH_BASE.'/includes/framework.php';

// Instantiate the application.
$app = JFactory::getApplication('site');

// Initialise the application.
$app->initialise();

First then you have access to the configured Joomla environment.

nibra
  • 3,958
  • 2
  • 20
  • 34
  • Using that i get: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /public_html/demo/irven/nkou_test.php:9) in /public_html/Joomla_2.5.9/libraries/joomla/session/session.php on line 532 Well, that's where I had the problem with the module-solution code. I'm pretty sure this warning is caused because I've already visited the joomla site and the session in the browser has started. – nkou Apr 05 '13 at 13:36
  • Do you use sessions outside of Joomla? – nibra Apr 05 '13 at 13:43
  • I use Virtuemart inside Joomla so I need to keep user's data throughout navigation... – nkou Apr 05 '13 at 13:52
  • Suppress the output in your script (output started at /public_html/demo/irven/nkou_test.php:9). That's preventing the session from being started. – nibra Apr 05 '13 at 13:57
  • The only php I have in nkou_test.php is your code. I think it has to do with the files that run after initialisation.. – nkou Apr 05 '13 at 14:08
  • 1
    @nkou you should put this on top of your file, before any html is included (even just whitespace), otherwise you will get `headers already sent...`. if you are including it from another file, the same rule apply - first include this code and initialize the app, before there is any html output – Marko D Apr 05 '13 at 14:39
1

Nibra! The code of yours is very similar to mine:

<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', '/home/user7633/public_html/cms' );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
JFactory::getApplication('site')->initialise();
$user = JFactory::getUser();
echo $user->username;
?>

The code done by this way does NOT work!

I am using Joomal 3.4.1 on PHP 5.4.36 and the code works only occasionally. Sometimes the username is empty and sometimes it is what excepted. I am logged in !

I am calling the PHP file from the "external URL" Menu Item.

Tickseeker
  • 141
  • 1
  • 8