Silex controller to Slim controller


Hi, i am new in Slim and get trouble to transform a Silex controller to work well :

public static function controller($app) {
$c = $app[‘controllers_factory’];
$c->get(’/browse/{path}’, function($path) use ($app) {
return self::browse($app, $path);
})->assert(‘path’, ‘.’);
$c->get(’/ajax/{path}’, function($path) use ($app) {
$target = DIR.’/…/data/’.$path;
if (!file_exists($target))
return self::msg(False, “$path does not exist”);
if (is_dir($target))
return self::get_content($app, $path);
return file_get_contents($target);
})->assert(‘path’, '.
$c->post(’/ajax/{path}’, function($path) use ($app) {
if ($_POST[‘type’] == ‘folder’)
return self::create_folder($path);
else if ($_POST[‘type’] == ‘file’)
return self::create_file($path);
else if ($_POST[‘type’] == ‘move’)
return self::move($_POST[‘src’], $_POST[‘dst’]);
else if ($_POST[‘type’] == ‘upload’)
return self::upload($path);
else if ($_POST[‘type’] == ‘edit’)
return self::save($path, $_POST[‘content’]);
return self::msg(False, ‘unknown type’);
})->assert(‘path’, ‘.’);
$c->delete(’/ajax/{path}’, function($path) use ($app) {
return self::remove($path);
})->assert(‘path’, '.
$c->get(’/’, function () use ($app) {
return $app->redirect(‘browse’);
return $c;

Any help will be more than apreciable :slight_smile:


Slim doesn’t really have a concept of controller in the same way Silex does. You can however rewrite your code to look more like:


$app->get(’/browse/{path}’, function($request, $response) use ($app) {
    // You will need to change self to the class name
    $content = self::browse($app, $request->getAttribute('path'));
    return $response->write($content);

$app->get(’/ajax/{path}’, function($request, $response) use ($app) {
    $path = $request->getAttribute('path');
    $target = DIR.’/…/data/’.$path;
    if (!file_exists($target))
        return $response->write(self::msg(False, “$path does not exist”));

    if (is_dir($target))
        return $response->write(self::get_content($app, $path));
        return $response->write(file_get_contents($target));

I have no idea what self is in these instances, but you will need to replace that with the class name instead.


Thank you, it’s working the way i wanted :slight_smile:

Still a small problem (in case you know the solution), I want Slim to want to return a file, in Silex I can use the sendFile helper method. It eases returning files that would otherwise not be publicly available. Simply pass it your file path, status code, headers and the content disposition and it will create a BinaryFileResponse response for you.
Does Slim have a similar function ?