Relaxing In Cyprus
Relaxing In Cyprus

Reputation: 2016

Accessing route::resource using https in Laravel 4

To force HTTPS on a named route, the Laravel docs say do the following:

Route::get('foo', array('https', function()
{
    return 'Must be over HTTPS';
}));

Now, on my first Laravel app, I have been using resource controllers. I don't think I will be using them for my second app, going on what I have since read, but for now they sit happily in my router.php file.

I wanted to force the back office part of my app to use HTTPS. So, my opening gambit was as follows:

Route::resource('backoffice', array('https','BackofficeController'));

Laravel didn't like the array.

So, instead I thought I would try putting at the next parameter:

Route::resource('backoffice', 'BackofficeController', 'https'));

But the next parameter needs to be an array. I could find no documentation on this, but I converted it to array. It still didn't work.

Route::resource('backoffice', 'BackofficeController', array('https')));

I even tried:

Route::resource('backoffice', 'BackofficeController', array('https'=>true)));

However, that failed too. So, how do I force a resource to use https?

Upvotes: 0

Views: 1192

Answers (2)

alou
alou

Reputation: 1502

Assuming you have a filter function like the one Andreyco suggested, which seems fine, you could do something similar to this:

//Andreyco's filter
Route::filter('forceHttps', function($req){
if (! Request::secure()) {
    return Redirect::secure(Request::getRequestUri());
}
});
//backoffice group routing
Route::group(array('prefix' => 'backoffice', 'before' => 'forceHttps'), function()
{
    Route::any('/',                'App\Controllers\BOindexController@index');
    Route::resource('otherBackOfficeURI', 'App\Controllers\OtherBOController');
    //other routes & controllers here...
});

This way, everything starting with site.tld/backoffice will go through the https filter (and most probably through a isAdmin filter) and then check inner function route rules. I think this will be more convenient.

Upvotes: 0

Andreyco
Andreyco

Reputation: 22872

Route::filter('forceHttps', function($req){
    if (! Request::secure()) {
        return Redirect::secure(Request::getRequestUri());
    }
});

Route::group(['before' => 'forceHttps'], function(){
    Route::resource('backoffice', 'BackofficeController');
});

Upvotes: 2

Related Questions