Getting Stated

This aims to provide a rounded general reference to PHPCR. You will mostly see code examples. It should work with any PHPCR implementation. We propose using Jackalope Jackrabbit to get started as it supports all features described here.

Installing Jackalope

Just follow the README of the jackalope-jackrabbit repository.

Browser to see what is in the repository

There are currently two options for browsing and modifying the contents of the PHPCR repository.

  • PHPCR Shell: Aims to provide a full command line shell interface to PHPCR content repositories. A pre-compiled PHAR archive is available on the github homepage.
  • Marmelab PHPCR Browser: A user-friendly web based PHPCR browser.

The shell is currently more feature complete, but the PHPCR Browser is more user friendly. We suggest you try both.

In a nutshell

The shortest self-contained example should output a line with ‘value’:


$factoryclass = '\Jackalope\RepositoryFactoryJackrabbit';
$parameters = array('jackalope.jackrabbit_uri' => 'http://localhost:8080/server');

// end of implementation specific configuration

// get a new PHPCR repository instance from the factory class defined above
$factory = new $factoryclass();
$repository = $factory->getRepository($parameters);

// create the credentials object to authenticate with the repository
$credentials = new \PHPCR\SimpleCredentials('admin', 'admin');

// login to the repository and retrieve the session
$session = $repository->login($credentials, 'default');

// retrieve the root node of the repository ("/")
$root = $session->getRootNode();

// add a new node
$node = $root->addNode('test', 'nt:unstructured');

// set a property on the newly created property
$node->setProperty('prop', 'value');

// save the session, i.e. persist the data

// retrieve the newly created node
$node = $session->getNode('/test');
echo $node->getPropertyValue('prop'); // outputs "value"

Still with us? Good, lets get in a bit deeper...

Get some data into the repository

We will now use the PHPCR import feature to import some initial data into the repository. First create an XML file called test.xml`:

<data xmlns:jcr="" xmlns:nt="">
    <node title="Test" content="This is some test content" />
    <sibling title="Test" content="This is another test content">
        <child1 title="Child1 title" />
        <child2 title="Child2 title" />
        <otherchild title="Otherchild title"/>
        <yetanother title="Yetanother title">
            <child title="Child title" />

Now import this into the repository:

$session->importXML('/', 'test.xml', \PHPCR\ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW);

You may also use the PHPCR Shell to import data:

phpcrsh -pmyprofile -c "session:import-xml test.xml"


The import feature is explored in the Import and export data chapter.