图像的Slim Framework 3路径/路由

时间:2018-10-26 16:02:04

标签: php slim-3

我具有以下结构:

index.php
/app
/images

我为所有不同的页面设置了路由设置,但是当我尝试直接访问图像时,它将尝试将该路由选择为我的动态路由之一。

因此,对于上下文,我正在从一个预先存在的应用程序进行转换,并维护它所具有的链接/路径,在索引末尾有一条完全动态的路由:

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

因此,当我在图像标签中执行以下操作时:

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

该路线启动,但失败。有什么方法可以让Slim忽略路径中带有“图像”的任何东西,而只提供图像?

我尝试在此处查看其他线程,但没有任何东西可以使我找到答案。

1 个答案:

答案 0 :(得分:1)

我似乎已通过以下方法解决了该问题:

$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);
});