2

[UserFrosting 0.3.1]

I want to execute a custom PHP file, bypassing other UserFrosting architecture.

Slim's $app->render("myfile.php") does not seem to work. It expects a twig file in the themes directory, it won't execute a PHP script.

How do I bypass this limitation?

Detailed information on what I am trying to achieve:

I have made a file-upload script in a custom PHP file. It uses the $_FILESarray, POSTed from a user form (from UserFrosting dashboard), to handle user file uploads and do some processing work.

I have not managed to get access to $_FILES data through a custom UserFrosting's controller class. Which is why I used a plain old external PHP file in root directory, and it works.

Now I am looking to route to that PHP file through Slim, to enforce basic user authentication/permissions.

Mafii
  • 7,227
  • 1
  • 35
  • 55
amivag
  • 91
  • 4

1 Answers1

2

In general, I would recommend against this approach for designing an application that will be easy to manage in the long term. $_FILES is a superglobal, and so should be accessible from anywhere - even from within a class method. So, I'm not sure why you would have trouble accessing it in your controller.

However, if you really need to invoke a standalone PHP file containing procedural code, you can always use a plain old include from within a route closure:

$app->get('/my-route/?', function () use ($app) {    

    // Access-controlled page
    if (!$app->user->checkAccess('uri_my_route')) {
        $app->notFound();
    }

    include "path/to/myfile.php";
});
alexw
  • 8,468
  • 6
  • 54
  • 86
  • Thanks. This worked for me. Even if I choose the MVC approach that your library adopts, is there any way I can separate my code from your library? Apologies if this sounds like a stupid question. I don't want to mess with your library and would rather keep my code in another folder for easier readability and convenience. Feel free to point out if I am missing something or doing it the wrong way. – input Aug 18 '16 at 17:36
  • @input See my answer to this question: http://stackoverflow.com/a/38724285/2970321 – alexw Aug 18 '16 at 17:48
  • The above method did not work out of the box, when I tried to route to a 3rd party system like phpMyAdmin (I include the main index.php file). I am getting "Uncaught ErrorException: require_once(./libraries/vendor_config.php): failed to open stream: No such file or directory.." and similar errors. phpMyAdmin works fine when outside the UserFrosting routing system. Will investigate more... Thank you – amivag Sep 12 '16 at 12:47