summaryrefslogtreecommitdiff
path: root/src/Http/LegacyUrlGenerator.php
blob: b9f8b7f178afd18722f3010efb67de384c6ea235 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

namespace Engelsystem\Http;

/**
 * Provides urls when webserver rewriting is disabled.
 *
 * The urls have the form <app url>/index.php?p=<path>&<parameters>
 */
class LegacyUrlGenerator extends UrlGenerator
{
    /**
     * @param string $path
     * @param array  $parameters
     * @return string urls in the form <app url>/index.php?p=<path>&<parameters>
     */
    public function to($path, $parameters = [])
    {
        $page = ltrim($path, '/');
        if (!empty($page)) {
            $page = str_replace('-', '_', $page);
            $parameters = array_merge(['p' => $page], $parameters);
        }

        $uri = parent::to('index.php', $parameters);
        $uri = preg_replace('~(/index\.php)+~', '/index.php', $uri);
        $uri = preg_replace('~(/index\.php)$~', '/', $uri);

        return $uri;
    }
}