Framework

Creating a Stand-alone Application

From Joomla! Documentation

(Redirected from How to create a stand-alone application using the Joomla! Framework)

This article will allow you to create your first Joomla Framework application, following a step-by-step process outlined below. We’ll assume you’re in an environment that has access to PHP (Mac OS X has this by default), and that you’ve got a connection to the Internet to download the Framework project.

Step by Step[edit]

Step 1: Download the latest Joomla Framework

Simply point your Web browser to http://github.com/joomla/joomla-Framework and then click on the “Downloads” button. In the pop-up window that appears, click on either the “Download .tar.gz” button or the “Download .zip” button, depending on your preference.

Step 2: Extract the Joomla Framework in your own environment

After you’ve downloaded the file, extract it in the location of your choosing.

Step 3: Create a new file

Create a folder named ‘examples’ within the root directory where you extracted the Joomla Framework. Using your favourite text/code editor, copy/paste code from the following examples (one at a time) into a new file. (Name the file as: ‘helloworld.php‘ or other name that you prefer).

Step 4: Execute your application from the command line interface (CLI)

Using your favourite CLI (maybe Terminal on the Mac, or PuTTy on Windows), navigate to the ‘examples’ folder where you saved your file. Type “php helloword.php” (or whatever you named your file) and click the enter key on your keyboard.

Step 5: Read the result on your screen, savor the moment

Step 6: Tweet your success

Tell the world that you just made your first successful Joomla Framework app by tweeting the following: “OMG, I just wrote a #jFramework app! #joomla”

Step 7: Add to the collection

Add your application to the example collection at Framework Examples


Problems? Check Framework Tips and Tricks

Examples[edit]

Example 1: Hello World Command Line Interface (CLI) App[edit]

<?php
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__));

require '../libraries/import.php';

class HelloWorld extends JApplicationCli
{
    public function execute()
    {
        $this->out('Hello World');
    }
}

JApplicationCli::getInstance('HelloWorld')->execute();

Example 2: Drawing cool things in the Terminal Screen[edit]

<?php
define('_JEXEC', 1);
 
require '../libraries/import.php';
 
class JoomlaRocks extends JApplicationCli
{
    public function execute()
    {
       $this->out('   .           .     .__       |      | ');
       $this->out('   | _  _ ._ _ | _.  [__) _  _.|_/ __ | ');
       $this->out('\__|(_)(_)[ | )|(_]  |  \(_)(_.| \_)  * ');
    }
}
 
JApplicationCli::getInstance('JoomlaRocks')->execute();

Example 3: Hello World using a var to save your name[edit]

<?php
define('_JEXEC', 1);

require '../libraries/import.php';

class HelloYou extends JApplicationCli
{
    public function execute()
    {
       $this->out('What is your name?');
       $name = $this->in();
  
       $this->out();
       $this->out('Hi '.$name.',');
       $this->out('Welcome to the Joomla Framework!');
       $this->out();
    }
  
    /**
    * We don’t really need configuration for this application.
    *
    * @return  void
    */
    protected function fetchConfigurationData()
    {
       return array();
    }
}

JApplicationCli::getInstance('HelloYou')->execute();

Example 4: Get your recent Tweets[edit]

<?php
define('_JEXEC', 1);

require '../libraries/import.php';

class TwitterFeed extends JApplicationCli
{
    public function execute()
    {
        $this->out('What is your twitter handle?');
        $username = $this->in();

        $this->out('How many tweets to view?');
        $count = $this->in();

        $tweets = $this->latest_tweet($username, $count);
        $this->out($tweets);
    }

    // Get Latest Tweet
    private function latest_tweet($username, $count = 5)
    {
        $url = 'http://twitter.com/statuses/user_timeline/'.$username.'.xml?count='.$count;

        $xml = simplexml_load_file($url) or die('could not connect');

        $text = '';
        
        foreach($xml->status as $status)
        {
            $text .= $status->text."\n";
        }

        return $text;
    }
}

JApplicationCli::getInstance('TwitterFeed')->execute();

Building a Web Application on the Framework[edit]

These instructions will allow you to create a minimal web application that does not use a database.

Problems? Check Framework Tips and Tricks

Download the latest version of the Joomla! Framework from github.

Create an index.php file in the root with the following starting code:

define('_JEXEC', 1);

// Fix magic quotes.
@ini_set('magic_quotes_runtime', 0);

// Maximise error reporting.
@ini_set('zend.ze1_compatibility_mode', '0');
error_reporting(E_ALL);
ini_set('display_errors', 1);

/*
 * Ensure that required path constants are defined.
 */
if (!defined('JPATH_BASE'))
{
	define('JPATH_BASE', realpath(__DIR__));
}

define('JPATH_LIBRARIES',		JPATH_BASE . '/libraries');

/**
 * Import the Framework. This file is usually in JPATH_LIBRARIES 
 */
require_once JPATH_LIBRARIES . '/import.legacy.php';

/**
 * Import the application.
 */
require_once JPATH_BASE.'/includes/application.php';


// Instantiate the application.
$web = MyWebApp::getInstance('MyWebApp');

// Run the application
$web->execute();

Now create a folder includes in the root and inside it a file application.php with the following code:

/**
 * Override the doExecute method from JApplicationWeb. Here you define what your application does. 
 */
class MyWebApp extends JApplicationWeb
{
		/**
		 * Display the application.
		 */
		protected function doExecute(){
			
			$this->setBody(
				'<h1>My Web Application</h1>'.
				'The current URL is '.JUri::current().'<br/>'.
				'The date is '. JFactory::getDate('now')
                	);
		}
}

If you browse to the web page you should see the heading followed by the two sentences, including the url and the date.