ben.oberholzer
ben.oberholzer

Reputation: 111

Slim framework 3 path/route for images

I have the following structure:

index.php
/app
/images

I have routes setup for all the different pages and whatnot but when I try to access an image directly it tries to pick that route up as one of my dynamic routes.

So for context, I am converting this from a pre-existing app and to maintain the links/paths that it had, I have a completely dynamic route at the end of the index:

$app->get('/{page}', 'HomeController:processRequest');
$app->get('/{page}/{id}', 'ProductController:viewProduct');
$app->get('/{page}/{id}/{show}', 'ProductController:viewProduct');

So when I do the following in an image tag:

https://example.com/images/nav/tinycheck.png

That route kicks in and it fails. Is there any way to tell Slim to ignore anything with 'images' in the path and just serve up the image?

I have tried looking at a bunch of other threads on here but nothing is pointing me to the answer.

Upvotes: 0

Views: 1118

Answers (1)

ben.oberholzer
ben.oberholzer

Reputation: 111

I seem to have resolved it with the following:

$app->get('/images/nav/{data}', function($request, $response, $args) {    
    $data = $args['data'];
    $image = @file_get_contents("/mypath/images/nav/".$data);
   if($image === FALSE) {
       $handler = $this->notFoundHandler;
       return $handler($request, $response);    
    }

    $response->write($image);
    return $response->withHeader('Content-Type', FILEINFO_MIME_TYPE);
});

Upvotes: 2

Related Questions