Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Question: FuelPHP global before or middleware
  • Hi,
    I have situation where i want a config entry which forces the use of https in site. When someone tries to access the site through http he will be redirected to the same page, but using https.
    I want to do it PHP side for better accessibility, no .htaccess this time, which would be easy.

    Appended my code at app/bootstrap.php then came across the issue that \Uri::current() always returns the base uri. By inspecting the core files found out that the issue might be related to the code not being executed within controllers, after Response class instance being forged.

    So my question is is there a global before method or some other kind of middleware i can place my code in?
  • It depends a bit on where and how you want to do it.

    You could do it in your index.php, if you want to check $_SERVER directly, and use that for redirects. If you want to use Fuel, you can do it in your app bootstrap, after the call to Fuel::init().
  • I'm currently doing it after Fuel::init(), but the Uri::current() still returns the base uri :/

    I want to do it the fuelphp-way to make the redirects user happy, as the \Uri::current() also will add parameters. I want the user to use to make the same request to the server just with HTTPS.
  • HarroHarro
    Accepted Answer
    The Uri class isn't of much help here, since that uses the active request to determine the current uri, and at that point there is no request yet.

    The only way to use the Uri class is to use a base controller (a class all your controllers extend), and use the before() in that base controller.

    I would however not do that, it requires the entire framework to be bootstrapped for a redirect, wasting CPU en memory that could be used for legitimate requests.

    Instead I would do something like in the index.php...
  • Well extending each controller isn't a real solution here. Ended up using the snippet of thestackoverflow post.

    Thanks for the response!
  • Tip for next time: always create a base controller, if only an empty one that extends \Controller (or whatever you want to use as a base).

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion