douzhicui2209 2013-08-10 12:42
浏览 98
已采纳

如何使用.htaccess在Laravel 4中选择环境?

I have a site that is in the testing phase, so it connects not to the real data but to a local copy of a database.

My customer needs now a fast link to retrieve some urgent and important pdf reports, so I need to set up another version of the site that links to the "real data".

I can create two environments ("local" and "real") with different database connections, and I would like to give the opportunity to select the environment via the URL (I know it's not pretty from a security point of view, but let's take this for granted).

So I would like to use:

my.ip.address/mysite to use the test db

my.ip.address/mysite/real to use the real db, redirecting to the URLs without the "real folders".

F.ex.: /mysite/real/admin should redirect to /mysite/admin but selecting the "real" environment on Laravel 4.

  1. is it possible to pattern-match also folders in Laravel 4 or is it possible only for domains? In other words, does this work?

    $env = $app->detectEnvironment(array(
        'test' => array('*/mysite/*'),
        'real' => array('*/mysite/real/*'),
    ));
    
  2. If so, I just can't figure out how to write the .htaccess rule, I've tried

    RewriteRule ^/real(.*)$ $1
    

But it won't work.

  • 写回答

2条回答 默认 最新

  • dsaxw4201 2013-08-10 12:54
    关注

    I'm not sure if Laravel's internal environment handling works with folders, but it definitely works with subdomains. You could choose to setup your app using two Apache vhosts, one named test.example.local, one named real.example.local and then set the environment option in Laravel like this:

    $env = $app->detectEnvironment(array(
        'test' => array('test.example.local'),
        'real' => array('real.example.local'),
    ));
    

    There's a second option using .htaccess: You could set this in your .htaccess...

    SetEnv LARAVEL_ENVIRONMENT test
    SetEnv LARAVEL_ENVIRONMENT real
    

    Then you could set your environment in Laravel like so:

    $env = $app->detectEnvironment(function()
    {
        return $_SERVER['LARAVEL_ENVIRONMENT'];
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题