Laravel 5.1只将url()函数链接到http

When I try to get the link of my site (for example domain.com), I just do url(); and it returns me http://www.domain.com. but what if I want to get a secure url (https) in my production and to get unsecured (http) in my local?

I don't want to use secure_url() and secure_asset() functions because I'm working on localhost too.

Update

Is there any trick I can do if I'm using cloudflare?

When I tried to add CloudFlare's ips in the Request::setTrustedProxies() method, It worked for me, I don't really know why but it fixed the problem..

Thank you all for helping! :)

You may try this:

url('foo/bar', [], app()->environment() !== 'local');

This'll produce a secure URL if the environment is not local so make sure you set local environment for development and production environment for live server.

Also, you may use app()->environment() == 'production' instead so only when environment is set to production then it'll produce a secure URL otherwise a non-secure URL will be generated.

Also, you may use something like these:

url('foo/bar', [], !app()->environment('local')); // if not local
url('foo/bar', [], app()->environment('production')); // if production
url('foo/bar', [], !app()->environment('local', 'staging')); // if not local/staging

Note: Don't forget to set the environment to local/production in env file.

Did you really try using url on both production and local development?

When you do url('foo/bar'); it checks whether your request is secure or not.

So if you access your website through http, all your urls will be http, if you access through https all your generated links will be over https.

This is the UrlGenerator

/**
 * Get the scheme for a raw URL.
 *
 * @param  bool|null  $secure
 * @return string
 */
protected function getScheme($secure)
{
    if (is_null($secure)) {
        if (is_null($this->cachedSchema)) {
            $this->cachedSchema = $this->forceSchema ?: $this->request->getScheme().'://';
        }

        return $this->cachedSchema;
    }

    return $secure ? 'https://' : 'http://';
}

Which will call this:

 /**
 * Checks whether the request is secure or not.
 *
 * This method can read the client protocol from the "X-Forwarded-Proto" header
 * when trusted proxies were set via "setTrustedProxies()".
 *
 * The "X-Forwarded-Proto" header must contain the protocol: "https" or "http".
 *
 * If your reverse proxy uses a different header name than "X-Forwarded-Proto"
 * ("SSL_HTTPS" for instance), configure it via "setTrustedHeaderName()" with
 * the "client-proto" key.
 *
 * @return bool
 */
public function isSecure()
{
    if ($this->isFromTrustedProxy() && self::$trustedHeaders[self::HEADER_CLIENT_PROTO] && $proto = $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PROTO])) {
        return in_array(strtolower(current(explode(',', $proto))), array('https', 'on', 'ssl', '1'));
    }

    $https = $this->server->get('HTTPS');

    return !empty($https) && 'off' !== strtolower($https);
}