【问题标题】:Accessing route::resource using https in Laravel 4在 Laravel 4 中使用 https 访问 route::resource
【发布时间】:2014-01-07 12:02:53
【问题描述】:

要在命名路由上强制使用 HTTPS,Laravel 文档说执行以下操作:

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

现在,在我的第一个 Laravel 应用程序中,我一直在使用资源控制器。我不认为我会将它们用于我的第二个应用程序,继续我读过的内容,但现在它们很高兴地坐在我的 router.php 文件中。

我想强制我的应用程序的后台部分使用 HTTPS。所以,我的开场白如下:

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

Laravel 不喜欢这个数组。

所以,我想我会尝试输入下一个参数:

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

但是下一个参数需要是一个数组。我找不到有关此的文档,但我将其转换为数组。还是不行。

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

我什至尝试过:

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

但是,这也失败了。那么,如何强制资源使用 https?

【问题讨论】:

    标签: php routing resources laravel


    【解决方案1】:
    Route::filter('forceHttps', function($req){
        if (! Request::secure()) {
            return Redirect::secure(Request::getRequestUri());
        }
    });
    
    Route::group(['before' => 'forceHttps'], function(){
        Route::resource('backoffice', 'BackofficeController');
    });
    

    【讨论】:

    • 感谢您的及时答复。我试过这个,它确实有帮助,但它仍然无法正常工作。如果我浏览到 hxxp://mysite/backoffice,它现在会重定向到 hxxps://mysite.backoffice,但是,我仍然收到 404 错误。
    • 抱歉,我还没完成就点击了添加评论。正如我在上面的编辑中解释的那样,如果我浏览到 hxxp://mysite/backoffice,它现在会重定向到 hxxps://mysite.backoffice,但是,我仍然会收到 404 错误。
    • Redirect::secure(...) 输出的 URL 是什么?你在本地虚拟主机上开发吗?如果有,网址是什么?我在我的项目中使用http://projectname.loc。测试了这个解决方案,它可以工作。
    • 生成的url是正确的。如果没有过滤器,并且在本地虚拟主机上进行了测试,如果我输入以下内容,我会进入我的后台:http://mysite.local/backoffice。但是,如果我应用过滤器并尝试上面的链接,它会被重定向到以下 url:https://mysite.local/backoffice 并生成 404 错误。
    • 这里好像是服务器配置问题。
    【解决方案2】:

    假设你有一个像 Andreyco 建议的过滤器功能,看起来不错,你可以做类似的事情:

    //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...
    });
    

    这样,以 site.tld/backoffice 开头的所有内容都将通过 https 过滤器(并且很可能通过 isAdmin 过滤器),然后检查内部函数路由规则。我觉得这样会更方便。

    【讨论】:

      猜你喜欢
      • 2014-06-23
      • 2013-10-06
      • 2016-02-18
      • 2019-06-14
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 2015-04-23
      相关资源
      最近更新 更多