diff options
Diffstat (limited to 'src/Http')
-rw-r--r-- | src/Http/LegacyUrlGenerator.php | 31 | ||||
-rw-r--r-- | src/Http/UrlGenerator.php | 30 | ||||
-rw-r--r-- | src/Http/UrlGeneratorInterface.php | 16 | ||||
-rw-r--r-- | src/Http/UrlGeneratorServiceProvider.php | 14 |
4 files changed, 91 insertions, 0 deletions
diff --git a/src/Http/LegacyUrlGenerator.php b/src/Http/LegacyUrlGenerator.php new file mode 100644 index 00000000..b9f8b7f1 --- /dev/null +++ b/src/Http/LegacyUrlGenerator.php @@ -0,0 +1,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; + } +} diff --git a/src/Http/UrlGenerator.php b/src/Http/UrlGenerator.php new file mode 100644 index 00000000..7ced769e --- /dev/null +++ b/src/Http/UrlGenerator.php @@ -0,0 +1,30 @@ +<?php + +namespace Engelsystem\Http; + +/** + * Provides urls when rewriting on the webserver is enabled. (default) + * + * The urls have the form <app url>/<path>?<parameters> + */ +class UrlGenerator implements UrlGeneratorInterface +{ + /** + * @param string $path + * @param array $parameters + * @return string url in the form [app url]/[path]?[parameters] + */ + public function to($path, $parameters = []) + { + $path = '/' . ltrim($path, '/'); + $request = app('request'); + $uri = $request->getUriForPath($path); + + if (!empty($parameters) && is_array($parameters)) { + $parameters = http_build_query($parameters); + $uri .= '?' . $parameters; + } + + return $uri; + } +} diff --git a/src/Http/UrlGeneratorInterface.php b/src/Http/UrlGeneratorInterface.php new file mode 100644 index 00000000..b3b6b12d --- /dev/null +++ b/src/Http/UrlGeneratorInterface.php @@ -0,0 +1,16 @@ +<?php + +namespace Engelsystem\Http; + +/** + * To switch between different URL schemes. + */ +interface UrlGeneratorInterface +{ + /** + * @param string $path + * @param array $parameters + * @return string + */ + public function to($path, $parameters = []); +} diff --git a/src/Http/UrlGeneratorServiceProvider.php b/src/Http/UrlGeneratorServiceProvider.php new file mode 100644 index 00000000..37304076 --- /dev/null +++ b/src/Http/UrlGeneratorServiceProvider.php @@ -0,0 +1,14 @@ +<?php + +namespace Engelsystem\Http; + +use Engelsystem\Container\ServiceProvider; + +class UrlGeneratorServiceProvider extends ServiceProvider +{ + public function register() + { + $urlGenerator = $this->app->make(UrlGenerator::class); + $this->app->instance('http.urlGenerator', $urlGenerator); + } +} |