Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route existing static html pages through symfony2

I currently have a live Symfony2.4 application that also links to a directory on our server (called "guides") which is under in the "web" folder. The files in this folder are generated automatically using an external program into plain html files by another department in our company.

Originally we had these files open to the public, but we'd like to make them only accessable if the user is logged into the application. We already have the site set up to handle the login process and such, I just can't figure out how to route the static files through Symfony so that I can have it use the Security process to check if the user is authorized to view the files.

I know Symfony is setup so that static files will automatically get served, so I modified the htaccess file to include this:

Anything that is a file and not in the "guide" directory should just get served.

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 that directory and go through a special controller where I can check for Authorized Users and redirect to a login if bad, or show the actual page if good.

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

The htaccess seems to work (i think) as it doesn't try to serve the page directly, but now I just get the Symfony 404 page.

like image 278
webjem Avatar asked Aug 19 '14 21:08

webjem


1 Answers

Once Symfony handles the request, I don't think it's possible to allow Apache to serve the files anymore. What you'll have to do is serve the requested file within the context of the Symfony framework.

Idea #1

One possible implementation would load the contents of the file into a Response object and return that from the appropriate Controller. The route definition looks good, so a very basic showAction method in the HelpGuide controller might look something like this:

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 new NotFoundHttpException("Guide {$filename} Not Found.");
    }
}

Symfony has some file management components (like the Finder class) that you could look into using if you elect to go this route.

Idea #2

Another, perhaps simpler implementation would be to have the Guide folder exist inside the twig template directory structure (or symlinked to/from there), where you can either render the requested file directly within the controller:

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

or load the file using the include tag within a simple "twig guide" loader template you create in the same folder.

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

...

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

Just some food for thought.

NOTE: I did not deal with authentication within the controller examples above. Naturally you'll need to incorporate whatever authentication method you've implemented. I assume you understand how to do that as your question did not directly inquire about authentication.

like image 70
Leo Bedrosian Avatar answered Oct 31 '22 20:10

Leo Bedrosian