简体   繁体   English

如何设置Laravel路由重定向到https?

[英]How to set Laravel Routes to redirect to https?

What is the correct way of setting some of my routes on Laravel 5.2 to be redireced using SSL? 将我在Laravel 5.2上的某些路由设置为使用SSL重新分配的正确方法是什么? http->https

Also, will I still be able to load http assets, or will firefox/chrome/IE have issues if I'm loading http components in a secure page? 另外,如果我在安全页面中加载http组件,我是否仍然可以加载http资产,或者firefox / chrome / IE会出现问题?

If you need to redirect to Https then there is an option in laravel redirect that you might like to know. 如果您需要重定向到Https则在laravel redirect中有一个您可能想知道的选项。

There is laravel helper for redirect called redirect() 有用于重定向的laravel帮助器,名为redirect()

check the ..vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\helpers.php file 检查..vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\helpers.php文件

if (! function_exists('redirect')) {
    /**
     * Get an instance of the redirector.
     *
     * @param  string|null  $to
     * @param  int     $status
     * @param  array   $headers
     * @param  bool    $secure
     * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
      */
    function redirect($to = null, $status = 302, $headers = [], $secure = null)
    {
        if (is_null($to)) {
            return app('redirect');
        }

    return app('redirect')->to($to, $status, $headers, $secure);
    }
}

check the parameters $to = null, $status = 302, $headers = [], $secure = null 检查参数$to = null, $status = 302, $headers = [], $secure = null

There is an option for Https as $secure , set it to true and there you will get an Https redirect for EX: redirect('/', 302, [], true) Https有一个$secure选项,将其设置为true,您将获得EX的Https重定向: redirect('/', 302, [], true)

NOTE: I also worked with this kind of thing and I did get an 404 error when the site is running on Https and assets loading trough Http as I remember, better to check it again. 注意: 我也使用这种方法,当我在网站上运行Https并通过Http加载资产时,确实收到了404 error ,最好再检查一次。 :) :)

there is a helper for loading secure assets secure_asset() 有一个用于加载安全资产的助手secure_asset()


I have faced some difficulties when testing in Http and production in Https , some assets are not loading and re-directions not working, What we did is create a separate helper to drop the schema , Http进行测试并在Https生产时,我遇到了一些困难,一些资产未加载, Https不起作用,我们要做的是创建一个单独的帮助程序来删除架构,

if (! function_exists('urlWithoutSchema')) {
    /**
     * Generate a url for the application without schema.
     *
     * @param  string  $path
         * @param  mixed   $parameters
     * @param  bool    $secure
     * @return string
     */
    function urlWithoutSchema($path = null, $parameters = array(), $secure = null)
    {
        $url = app('url')->to($path, $parameters, $secure);

        return str_replace(parse_url($url, PHP_URL_SCHEME).':', '', $url);
    }
}

THEN urlWithoutSchema("https://laravel.com/") will results in //laravel.com/ 然后urlWithoutSchema("https://laravel.com/")将导致//laravel.com/

IF you use redirect(urlWithoutSchema("your-site-name.com")) then it will redirect to //your-site-name.com/ that means if you using Https site will redirect with Https otherwise to Http . 如果您使用redirect(urlWithoutSchema("your-site-name.com"))那么它会重定向到//your-site-name.com/如果你使用,这意味着Https网站将重定向Https否则Http

Same thing can be use for loading assets like, 同样的东西可以用于加载资产,例如,

<link href="{{ urlWithoutSchema('css/vendor.css') }}" rel="stylesheet" />

Then when you running on Http css/vendor.css will load through Http , and if you running on Https assets will load via Https . 然后,当你运行Http css/vendor.css将通过加载Http ,如果你运行Https资产将通过加载Https

Using this you can avoid some issues regurding environments which has Https and which hasn't Https 使用此方法,可以避免在具有Https和没有Https环境中出现一些问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM