3

We have a simple route defined like the following one:

Route

// Home
Route::get('/home', [
    'as' => 'home::index',
    'uses' => 'IndexController@home'
]);

View

<a href="{{ route('home::index') }}">Home</a>

We need to force our link/route in view rendered as HTTP or in some cases as HTTPS route e.g. http://host.domain/home or https://host.domain/home.

We cannot use URL::forceSchema("http") or URL::forceSchema("https") since we need to force HTTPS on a HTTP page and HTTP on a HTTPS page. We have a multidomain application. Some domains running via HTTP some via HTTPS. A link to different domain / "application section" can be placed everywhere. A domain running via HTTPS cant be accessed via HTTP. A domain running on HTTP cant be accessed via HTTPS

How to force a route rendered as a specific hypertext protocol?

lin
  • 17,956
  • 4
  • 59
  • 83
  • @Heyne before marking an question as duplicate, please read the question carefully and also check the question you marked as duplicated. Your marked answer does not solve this problem in any way. – lin Aug 02 '18 at 12:23
  • How do you decide which schema should be used? This does not seem to be a view related logic. I guess you want the route callback to produce two different responses if called over http or https, am I correct? – Nima Aug 02 '18 at 14:16
  • @Nima We have a multidomain application. Some domains running via HTTP some via HTTPS. A link to different domain / "application section" can be placed everywhere. A domain running via HTTPS cant be accessed via HTTP. A domain running on HTTP cant be accessed via HTTPS. – lin Aug 02 '18 at 14:41
  • I think you can force a route to be served over http/https only, that will become a property of the route and it is used when generating url to the route. Will that solve your problem? – Nima Aug 02 '18 at 14:53
  • @Nima yes, this would solve our problem. – lin Aug 02 '18 at 14:56

1 Answers1

2

There is a section in Laravel documentaion under title Forcing A Route To Be Served Over HTTPS. It does not mention generated URL for the route, but I can see in Illuminate\Routing\UrlGenerator code this settings is respected in getRouteScheme method. So adding a new value http/https to the action array should do the trick:


Force route to be rendered as HTTP:

Route::get('/home', [
    'as' => 'home::index',
    'uses' => 'IndexController@home',
    'http'
]);

Force route to be rendered as HTTPS:

Route::get('/home', [
    'as' => 'home::index',
    'uses' => 'IndexController@home',
    'https'
]);

Now route('home::index') should generate URL based on schema you defined.

lin
  • 17,956
  • 4
  • 59
  • 83
Nima
  • 3,309
  • 6
  • 27
  • 44