Forced silex structure for generating https links

My entire application must be accessible via HTTPS, Vhost is configured correctly, and when I call any of my application pages with the HTTPS prefix, it responds correctly.

However, when I try to access the page through the generated link in my application. it always points to a regular HTTP link.

At first, I tried to redirect all traffic to HTTPS using the .htaccess file. Spent a few hours trying to use every redirection method I could find on the network, but for some reason I always had an endless redirect cycle.

Now I'm trying to approach this in a different way: direct link building in HTTPS. All my links are created using the function URL()provided by the branch bridge.

Pretty easy to set up with symfony

But I could not figure out how to do this with Silex. Is there an easy way to change the routing scheme in Silex to force every link to be generated using the HTTPS prefix?

+4
source share
5 answers

Adding requireHttps()routes to your generator will do the trick.

Example:

$app->get('/hello-world', function($app) {
    return 'Hello world';
})
->bind('hello') // Set route name (optional of course)
->requireHttps(); // This would force your url with https://

You can look at the API for more information. Here you will find everything you need.

+3
source

Set the environment variable HTTPSto on.

Nginx configuration in place: fastcgi_param HTTPS on;
Apache.htaccess or vhost config:SetEnv HTTPS on

, nginx off, https-, :
http://silex.sensiolabs.org/doc/web_servers.html
Ctrl + F - fastcgi_param HTTPS off;

+1

, requireHttps() Controller, HTTPS :

namespace Acme;

use Silex\Application;
use Silex\Api\ControllerProviderInterface;

class HelloControllerProvider implements ControllerProviderInterface
{
    public function connect(Application $app)
    {
        $controllers = $app['controllers_factory'];
        $controllers->get('/', function (Application $app) {
            return $app->redirect('/hello');
        });
        $controllers->get('/another-page', 'Acme\HelloControllerProvider::doSomething')->bind('another-page');
        // here it goes
        $controllers->requireHttps();
        // or even make it more flexible with a config variable
        // if ($app['config']['require_https']) (
        //     $controllers->requireHttps();
        // }
        return $controllers;
    }
}
+1

, url, , app.php:

$app['customBaseUrl'] = 'https://mybaseurl';

baseUrl. twil url().

, . Artamiel , .

0

Another option is to use access_control in the SecurityServiceProvider setting (if you use this) to indicate that certain routes require https by specifying 'require_channel' => 'https' in the access_control section of the security configuration.

0
source

All Articles