1

This is the helper function route() in laravel

function route($name, $parameters = [], $absolute = true)
    {
        return app('url')->route($name, $parameters, $absolute);
    }

how could i override that function so $absolute = false by default?

spqa
  • 186
  • 1
  • 11
  • 1
    `route('name', [], false]);` – Derek Pollard Jun 14 '17 at 17:34
  • i mean i can use route('name',[]) as normal without set that parameter , is that possible? – spqa Jun 14 '17 at 17:38
  • You could write a new helper function that uses `route()` internally. Why, though? What's wrong with the absolute links? – ceejayoz Jun 14 '17 at 17:39
  • Yeah, you could even set parameters to null, as it will default to `[]` like so: `route('name', null, false);` – Derek Pollard Jun 14 '17 at 17:39
  • i force https in cloudflare and when i use route in form the url is http so i cant submit form – spqa Jun 14 '17 at 17:41
  • @Derek The `route` helper is part of Laravel. OP doesn't control its defaults. – ceejayoz Jun 14 '17 at 17:41
  • @ceejayoz yes he can https://laravel.com/docs/5.4/helpers#method-route – Derek Pollard Jun 14 '17 at 17:42
  • @Derek He wants to change the defaults, i.e. not have to specify `false` every time he uses it. That'd require updating the code in `vendor`, which you can't do. – ceejayoz Jun 14 '17 at 17:43
  • @ceejayoz he can control the defaults by *passing* them to the function, as we see in the examples from the documentation: `$url = route('routeName', ['id' => 1], false);` – Derek Pollard Jun 14 '17 at 17:43
  • @spqa You should handle the `X-Forwarded-Proto` header Cloudflare sends correctly, instead of this kludgy workaround. In Apache, you need `SetEnvIf x-forwarded-proto https HTTPS=on` in your VirtualHost config. nginx can do similar. – ceejayoz Jun 14 '17 at 17:43
  • @ceejayoz you are right , but that is not a good practice since it is in gitignore – spqa Jun 14 '17 at 17:44
  • @Derek OP *clearly* wants a `route('whatever')` call to get `false` by default instead of the usual `true`. OP clearly knows the options are *available*, as he's cited those params in the question itself. – ceejayoz Jun 14 '17 at 17:44

1 Answers1

0

i force https in cloudflare and when i use route in form the url is http so i cant submit form

Cloudflare sends an X-Forwarded-Proto header that should be correctly handled to tell your server it's being accessed over HTTPS.

In Apache, adding this to your VirtualHost should tell Laravel it's being accessed securely and it'll automatically generate the correct URLs:

SetEnvIf x-forwarded-proto https HTTPS=on
ceejayoz
  • 176,543
  • 40
  • 303
  • 368