php - 在 Laravel 中自定义带有前缀的 URL

标签 php laravel url-routing

我有这个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/

相关文章:

拉拉维尔 : Pass information with key in url

laravel - 如何在不使用路由的情况下管理 React Js 到多页面应用程序?

regex - 如何创建处理如下 URL 的 Express 路由?

javascript - 在应用程序初始化后添加 Angular ionic View 和路由

regex - 如何在 golang 中使用正则表达式获取 url 模式?

php - WordPress更改特色图片默认大小

php - MySQL/PHP 更新查询未执行

javascript - 如何从表单元素的值中的数组中获取数组元素?

php - 使用 pg_insert() 后如何确定插入行的 pkey?

angularjs - 如何在 git 存储库中拆分 Angular 应用程序客户端/服务器端代码