如何使我的PHP应用程序url在机器和网络之间可移植?


How can I make my PHP application URL's portable across machines and networks?

我有一个使用前端控制器模式的PHP web应用程序,带有"漂亮的url ",例如http://example.com/home/index/params

应用程序使用干净的URI -与Apache在后台重写,例如:上述请求将在内部重定向到

http://example.com/index.php?url=home/index/params

然而,在这种情况下,浏览器认为内容来自一个名为index目录下名为params的文档,因此任何其他请求,例如style.css将被发送到

`http://example.com/home/index/style.css` 

这当然是错误的,所以我在PHP中使用一个变量来硬编码路径请求

// constants.php
define('BASE_URL', 'http://localhost/03_my_website_projects/v4/public/')
// views.php
<link href="<?php echo BASE_URL; ?>styles.css" rel="stylesheet">

生产

<link href="http://localhost/03_my_website_projects/v4/public/styles.css" rel="stylesheet">

问题是,当我试图通过本地网络访问移动设备上的网站时,我无法查看,因为所有链接都硬编码为URL地址,只能从项目正在开发的机器上工作。例如,我的移动设备希望看到像

这样的路径
<link href="http://192.168.1.6/03_my_website_projects/v4/public/styles.css" rel="stylesheet">

如何使URL在机器和网络之间可移植?

我知道Laravel框架对此有一个非常好的解决方案(我希望我知道它是什么),但是这个黑客解决方案是我能想到的唯一一件事,将生存DHCP IP地址重新分配

// file: define_constants.php
$ip_address = shell_exec("ip addr show wlan0 | grep 'inet's' | awk '{ print $2; }' | sed 's/'/.*$//'");
define('BASE_URL', 'http://' . $ip_address . '/03_my_website_projects/v4/public/');

现在所有的链接都将使用这个新地址,例如

 <link href="http://192.168.1.6/03_my_website_projects/v4/public/styles.css" rel="stylesheet">