diff options
Diffstat (limited to 'src/Routing')
-rw-r--r-- | src/Routing/LegacyUrlGenerator.php | 31 | ||||
-rw-r--r-- | src/Routing/RoutingServiceProvider.php | 14 | ||||
-rw-r--r-- | src/Routing/UrlGenerator.php | 11 | ||||
-rw-r--r-- | src/Routing/UrlGeneratorInterface.php | 16 |
4 files changed, 67 insertions, 5 deletions
diff --git a/src/Routing/LegacyUrlGenerator.php b/src/Routing/LegacyUrlGenerator.php new file mode 100644 index 00000000..fdac4f96 --- /dev/null +++ b/src/Routing/LegacyUrlGenerator.php @@ -0,0 +1,31 @@ +<?php + +namespace Engelsystem\Routing; + +/** + * 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 linkTo($path, $parameters = []) + { + $page = ltrim($path, '/'); + if (!empty($page)) { + $page = str_replace('-', '_', $page); + $parameters = array_merge(['p' => $page], $parameters); + } + + $uri = parent::linkTo('index.php', $parameters); + $uri = preg_replace('~(/index\.php)+~', '/index.php', $uri); + $uri = preg_replace('~(/index\.php)$~', '/', $uri); + + return $uri; + } +} diff --git a/src/Routing/RoutingServiceProvider.php b/src/Routing/RoutingServiceProvider.php index b7db1383..beaa6a94 100644 --- a/src/Routing/RoutingServiceProvider.php +++ b/src/Routing/RoutingServiceProvider.php @@ -1,14 +1,24 @@ <?php - namespace Engelsystem\Routing; use Engelsystem\Container\ServiceProvider; +/** + * Registers the url generator depending on config. + */ class RoutingServiceProvider extends ServiceProvider { + public function register() { - $urlGenerator = $this->app->make(UrlGenerator::class); + $config = $this->app->get('config'); + $class = UrlGenerator::class; + if (! $config->get('rewrite_urls', true)) { + $class = LegacyUrlGenerator::class; + } + + $urlGenerator = $this->app->make($class); $this->app->instance('routing.urlGenerator', $urlGenerator); + $this->app->bind(UrlGeneratorInterface::class, 'routing.urlGenerator'); } } diff --git a/src/Routing/UrlGenerator.php b/src/Routing/UrlGenerator.php index 6df52425..188bac3b 100644 --- a/src/Routing/UrlGenerator.php +++ b/src/Routing/UrlGenerator.php @@ -2,14 +2,19 @@ namespace Engelsystem\Routing; -class UrlGenerator +/** + * 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 + * @return string url in the form [app url]/[path]?[parameters] */ - public function to($path, $parameters = []) + public function linkTo($path, $parameters = []) { $path = '/' . ltrim($path, '/'); $request = app('request'); diff --git a/src/Routing/UrlGeneratorInterface.php b/src/Routing/UrlGeneratorInterface.php new file mode 100644 index 00000000..f1a8ffed --- /dev/null +++ b/src/Routing/UrlGeneratorInterface.php @@ -0,0 +1,16 @@ +<?php + +namespace Engelsystem\Routing; + +/** + * To switch between different URL schemes. + */ +interface UrlGeneratorInterface +{ + /** + * @param string $path + * @param array $parameters + * @return string + */ + public function linkTo($path, $parameters = []); +} |