PSX Framework

Location: PSX Framework / Home /

Welcome,

PSX is a framework for developing dynamic websites in PHP. It bootstraps your project by providing a clean folder structure and a light MVC framework to develop clean and readable code. PSX helps you developing RESTful APIs serving web standard formats like JSON, XML, Atom and RSS. It has a focus on social technologies and provides classes to use and implement OAuth, OpenID, Opengraph, Opensocial, Opensearch, PubSubHubbub, WebFinger, Atom, and RSS. At the example page you can see sample implementations using various PSX classes wich give you an good overview how the PSX framework works. On the download section you can grab the current release of PSX. If you want contribute or get in contact you find at the community page all necessary informations. But enough words in the following some code to see how PSX works.

Developing clean and readable code

In this example we can access the controller with index.php/news or index.php/news/1 to get a single news. To add a new news title you have to send an POST request to index.php/news/submit. All dependencies wich are used in the controller are created in the dependency container "Dependency\Example". In this example we have the following dependencies template, post, validator, table. Through the dependency container it is possible to manage precisely all dependencies for each controller. As default template engine PSX uses simply PHP but you can use any other template engine.

<?php
use PSX\DateTime;
use PSX\Dependency;
use PSX\Filter;
use PSX\Module\ViewAbstract;
use PSX\Sql\Condition;

class news extends ViewAbstract
{
    /**
     * @httpMethod GET
     * @path /{newsId}
     */
    public function getNews()
    {
        $newsId = $this->getUriFragments('newsId');
        $con    = null;

        if (!empty($newsId)) {
            $con = new Condition(array('id', '=', $newsId));
        }

        $result = $this->table->getAll(array('id', 'title', 'date'), $con);

        $this->template->assign('result', $result);
        $this->template->set('news.tpl');
    }

    /**
     * @httpMethod POST
     * @path /submit
     */
    public function addNews()
    {
        $title = $this->post->title('string', array(new Filter\Length(3, 16)));

        if (!$this->validator->hasError()) {
            $this->table->insert(array(
                'title' => $title,
                'date'  => date(DateTime::SQL)
            ));
            $this->template->assign('success', true);
        } else {
            $this->template->assign('errors', $this->validator->getError());
        }

        $this->template->set('news.tpl');
    }

    public function getDependencies()
    {
        return new Dependency\Example($this->base->getConfig());
    }
}

Easy dependency managment

<?php
namespace PSX\Dependency

use PSX\DependencyAbstract;
use PSX\Sql;

class Example extends DependencyAbstract
{
    protected function setup()
    {
        parent::setup();

        $this->getSql();
    }

    public function getSql()
    {
        if ($this->has('sql')) {
            return $this->get('sql');
        }

        return $this->set('sql', new Sql($this->config['psx_sql_host'],
            $this->config['psx_sql_user'],
            $this->config['psx_sql_pw'],
            $this->config['psx_sql_db']));
    }

    // ...
}

All dependencies in PSX are defined in an DI container class wich extends the class PSX\DependencyAbstract. This shows the DI container wich is used in the previous example. Every object wich was set in the setup() method is available in the controller as attribute. Normally we define in an project a base dependency where we setup the classes wich are needed in every controller like sql and template classes. From there we can extend the dependencies to more specific ones i.e. creating a user object.

Building RESTful APIs

PSX offers a set of powerful data managment classes wich allows you to build webservices wich can serve different formats i.e. XML, JSON, RSS and Atom. In this example we use the PSX SQL ORM library to obtain an PSX\Data\ResultSet. By calling the setResponse() method the ResultSet is converted into XML. If we dont specify the GET parameter format PSX will check the Accept header and will try to find the best writer to serve the fitting format. If you visit the page with an browser you will most likely receive XML because most browsers send an application/xml content type in the Accept header.

We can also get the current request with the method getRequest(). PSX tries to find the fitting reader depending on the Content-Type header of the request. You can import the request into any class wich implements the PSX\Data\RecordInterface. After this the record can be easily inserted into the news table.

GET /news?format=xml
<?php
use PSX\Module\ApiAbstract;
use PSX\Sql;

class news extends ApiAbstract
{
    public function onGet()
    {
        $result = $this->table->select(array('id', 'title', 'date'))
            ->orderBy('date', Sql::SORT_DESC)
            ->getAll(Sql::FETCH_OBJECT);

        $this->setResponse($result);
    }

    public function onPost()
    {
        $news = new News();
        $news->import($this->getRequest());

        $this->table->insert($news);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<resultset>
 <totalResults>1</totalResults>
 <startIndex>0</startIndex>
 <itemsPerPage>16</itemsPerPage>
 <entry>
  <id>1</id>
  <title>foobar</title>
  <date>2012-09-28 20:56:33</date>
 </entry>
</resultset>

Awesome libraries

<?php
use PSX\Atom;
use PSX\Http;
use PSX\Module\ViewAbstract;

class foo extends ViewAbstract
{
    /**
     * @httpMethod GET
     * @path /
     */
    public function sendHttpPostRequest()
    {
        $http = new Http();
        $request = new Http\PostRequest('http://localhost', null, array(
            'user' => 'foo', 
            'pw' => 'bar'
        ));
        $response = $http->request($request);

        if ($response->getCode() == 200) {
            // login succcessful
            echo $response->getHeader('Cookie') . "\n";
            echo $response->getBody();
        }
    }

    /**
     * @httpMethod GET
     * @path /feed
     */
    public function parseAtomFeed()
    {
        $atom = Atom::request('http://test.phpsx.org/index.php/atom');

        echo $atom->title:
        echo $atom->updated;

        foreach ($atom as $entry) {
            echo $entry->title . "\n";
        }
    }
}

PSX has a set of uniqe libraries wich makes the daily development easier. It includes classes to use and implement OAuth, OpenID, Opengraph, Opensocial, Opensearch, PubSubHubbub, WebFinger, Atom, and RSS. Here only a few short examples of some PSX classes in action. Please see the example page for more details.

by last modified on