Routing existing static html pages via symfony2

Currently, I have a Symfony2.4 application that also links to a directory on our server (called "directories") located in the "Internet" folder. Files in this folder are automatically generated using an external program into simple html files by another department of our company.

Initially, we had these files open to the public, but we would like to make them available only if the user is registered in the application. We already have a site configured to handle the login process, etc. I just can't figure out how to route static files through Symfony so I can use this security process to check if the user is allowed to view files.

I know that Symfony is configured so that static files are automatically maintained, so I modified the htaccess file to include this:

All that is , and the file is not in the directory "guide" should just be serviced.

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} !/guides/
RewriteRule .? - [L]

Then I tried to make a route that can catch all the URLs coming from this directory and go through a special controller where I can check Authorized users and redirect to login if it’s bad, or show the actual page if it’s good.

support_guides:
  path: /guides/{the-rest}
  defaults: { _controller: MillenniumSIMeevoSupportBundle:HelpGuide:show }

Htaccess seems to work (I think) as it is not trying to serve the page directly, but now I just get the Symfony 404 page.

+4
source share
1 answer

As soon as Symfony processes the request, I don’t think that you can let Apache serve the files anymore. What you need to do is execute the requested file in the context of the Symfony structure.

Idea number 1

Response . , showAction HelpGuide :

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

...

public function showAction($filename)
{
    $file = "/path/to/file/{$filename}";

    if (file_exists($file)) {
        return new Response(file_get_contents($file));
    } else {
        throw NotFoundHttpException("Guide {$filename} Not Found.");
    }
}

Symfony (, Finder), .

№ 2

, , , Guide twig ( /), :

// MillenniumSIMeevoSupportBundle:HelpGuide controller
return $this->render("MillenniumSIMeevoSupportBundle:HelpGuide:{$filename}.html");

"twig guide", .

// MillenniumSIMeevoSupportBundle:HelpGuide controller
return $this->render('MillenniumSIMeevoSupportBundle:HelpGuide:guideloader.twig.html', array('filename' => $filename));

...

// guideloader.twig.html
{% include filename ignore missing %}

.

. . , , . , , , .

0

All Articles