ZF4 Blog

Creating an XSD Server Using Slim

Following the work I did yesterday to create Zend ACL definitions from XML files, it followed that I needed to host the XSD in a public place. The Slim Framework is ideal for these jobs, so here it is:

use Slim\Slim;

$app = new Slim();
$app->xsdDir = realpath(__DIR__ . '/..') . '/xsd';

/**
 * Define routes
 */
$app->get('/schema/:name',
        function($name) use ($app) {
            $file="{$app->xsdDir}/{$name}.xsd";
            if (!file_exists($file)) {
                $app->halt("{$name} does not exist");
            }
            $app->response->setBody(file_get_contents($file));
            $app->response->headers->set("Content-type", "application/octet-stream");
        })->name('schema');

$app->run();

Now this is very raw, and now doubt can benefit from some tarting up, getting a nice homepage display etc, but in essence it works. Basically, store your XSD files under the public root in the xsd directory and then call the site with /schema/name e.g.

http://schema.zf4.biz/schema/zendacl