Building Titanium with PHP

Several months ago, I started the process of embedding PHP into the Titanium platform. After several sprints of work, the Appcelerator team was finally ready to merge my work into their master branch, and Martin Robinson worked to bring the implementation to maturity. The current release of Titanium Developer does not yet have the PHP support built in to the bundled SDK, and I’m not sure when it will be released, so I thought I’d share how to get a development version built with PHP support so you can start playing with it and even help the Appcelerator team by catching bugs in the implementation.

If you’re not familiar with Titanium, it’s a development platform that allows you to build native desktop applications for Windows, Mac OS X, and Linux using the Web technologies we use on a daily basis: HTML, JavaScript, CSS, Python, Ruby, and now PHP. Titanium is built on top of WebKit, and it includes a micro-kernel called Kroll that does all of the pre-processing and runtime processing of the code. Languages are modules in Kroll and are first-class citizens in the runtime environment. So, when using the runtime, you can pass objects back and forth between JavaScript, PHP, Python, and Ruby. This, however, can’t be done when using pre-processing mode, but in that case, your PHP scripts will work just like you’re used to them working on a web server.

Since it may still be a while before the next release of Titanium that includes PHP, I’ve decided to provide instructions here on how to build the SDK from the master branch and connect it to the Titanium Developer application. Sorry, but I don’t have instructions for Windows; these work on Mac or Linux.

  1. Download (and install) Titanium Developer from http://www.appcelerator.com/products/download/

  2. Launch Titanium Developer and let it do its thing (you’ll have to create an acct)

  3. Now you may close it

  4. You should now have a Titanium directory with the SDK in it at either:

    • /Library/Application\ Support/Titanium/ (Mac)
    • ~/.titanium/ or /opt/titanium/ (Linux)
  5. Now, build the development SDK to use with Titanium Developer. You’ll need Python, scons, a compiler, and possibly other prerequisites.

    $> git clone git://github.com/appcelerator/titanium_desktop.git
    $> cd titanium_desktop/
    $> git submodule update --init
    $> scons dist
  6. Unzip the built SDK to some location. I like to use ~/.titanium-dev/.

    $> unzip build/osx/dist/sdk-0.7.0.zip -d ~/.titanium-dev/
  7. Move Titanium Developer SDK out of the way and point to your build SDK

    $> cd /Library/Application\ Support/Titanium/
    $> mv modules/ modules-old/
    $> mv runtime/ runtime-old/
    $> mv sdk/ sdk-old/
    $> ln -s ~/.titanium-dev/modules modules
    $> ln -s ~/.titanium-dev/runtime runtime
    $> ln -s ~/.titanium-dev/sdk sdk
  8. Now relaunch Titanium Developer.

You’re now ready to build a project with PHP!

Click Create to generate a stub project. I’ll cover more about development with PHP in Titanium in the future, but let’s get started by showing a phpinfo() page. Open the index.html file in the Resources directory of your project and add the following anywhere in the page:

<p><a href="phpinfo.php" style="color:gray;">See the phpinfo()</a></p>

In the Resources folder, create the phpinfo.php script and add the following to that script:

<?php phpinfo(); ?>

Now, go back to Titanium Developer, click the Test & Package tab for your project, then click the Launch tab. To build and run your application for testing, click the Launch App button.

You should now see a dark gray test page with the “See the phpinfo()” link you created. Click that link, and it should take you to the phpinfo.php script you created with the proper phpinfo() output. Congratulations!

Visit the Appcelerator Codestrong developer center for more information on building Titanium applications. Visit #titanium_app on Freenode IRC to ask questions about application development. If you want to help develop the project and contribute PHP tests to it, join #titanium_dev.