我有这个html:
<ul>
<li><a href="index.php/Page1">Page 01</a></
<li><a href="index.php/Page2">Page 02</a></li>
<li><a href="index.php/Page3">Page 03</a></li>
</ul>
如您所见,我需要使用 index.php/
由于我大学的服务器,所有链接中都有前缀
(我无法改变它)。按照上面的方法,从主页直接转到任何页面都可以,但是如果我尝试从另一个页面访问某个页面,则会得到错误的 URL,并且无法访问该页面:
示例:
首页
http://localhost/php-project/public/
第1页(从家里)
来自:http://localhost/php-project/public/
至:http://localhost/php-project/public/index.php/Page1
第2页(从家里)
来自:http://localhost/php-project/public/
至:http://localhost/php-project/public/index.php/Page2
第 1 页(来自第 2 页)
来自:http://localhost/php-project/public/index.php/Page2
至:http://localhost/php-project/public/index.php/index.php/Page1
如您所见,前缀会重复出现。我不知道如何才能正常工作。
有什么想法吗?
最佳答案
您可以使用Illuminate\Routing\UrlGenerator
类上的forceRootUrl
方法来设置它。
示例:
// app/Providers/AppServiceProvider
public function boot()
{
// Instance of Illuminate\Routing\UrlGenerator
$urlGenerator = $this->app['url'];
// Instance of Illuminate\Http\Request
$request = $this->app['request'];
// Grabbing the root url
$root = $request->root();
// Add the suffix
$rootSuffix = '/index.php';
if (!ends_with($root, $rootSuffix)) {
$root .= $rootSuffix;
}
// Finally set the root url on the UrlGenerator
$urlGenerator->forceRootUrl($root);
}
关于php - 在 Laravel 中自定义带有前缀的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35234328/