使用 homestead vagrant 解决方案访问 symfony 的 app_dev.php

Accessing symfony's app_dev.php using homestead vagrant solution

我已将宅基地设置为 documentation 状态,并对 Homestead.yaml 进行了以下更改以处理我的项目:

ip: 192.168.10.10
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
    - ~/.ssh/id_rsa
folders:
    -
        map: /home/pcmagas/Kwdikas/php/apps/ellakcy_member_app/
        to: /home/vagrant/code
sites:
    -
        map: homestead.test
        to: /home/vagrant/code/web
        type: symfony

databases:
    - homestead
name: ellakcy-member-app
hostname: ellakcy-member-app

但是当我通过浏览器访问时 http://192.168.10.10/ 我收到以下错误:

You are not allowed to access this file. Check app_dev.php for more information.

当您查看 app_dev.php 时,有以下代码片段:

if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !(in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'], true) || PHP_SAPI === 'cli-server')
) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}

这意味着 app_dev.php 脚本无法识别正在访问的 ip,因此为了防止在开发期间进行任何授权访问,它会阻止加载 symfony。

为了绕过这种保护,你应该弄清楚哪个是你的客户端在 vagrant 虚拟网络上的 ip,快速而肮脏的方法是用以下代码片段替换上面的片段:

if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !(in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'], true) || PHP_SAPI === 'cli-server')
) {
    header('HTTP/1.0 403 Forbidden');
    echo 'REMOTE_ADDR: '.$_SERVER['REMOTE_ADDR']."<br>";
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}

如您所见,它打印出玩具应放置在数组中的正确值:

!(in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'/*, Found Ip goes there*/], true)

我的情况是 192.168.10.1,我想你的情况也应该是这样。