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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?