diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-03-31 05:19:49 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-05 21:46:05 +0200 |
commit | 6962c2b790b5428699fe5897de840d41090fcd37 (patch) | |
tree | 646a1dca1db5e801f70733ce6b30d8682241207f /src | |
parent | 9ca36bed3446cdd4060d7d6fa1025512d0922e0e (diff) |
Legacy URL Support
Diffstat (limited to 'src')
-rw-r--r-- | src/Routing/LegacyUrlGenerator.php | 25 | ||||
-rw-r--r-- | src/Routing/RoutingServiceProvider.php | 9 | ||||
-rw-r--r-- | src/Routing/UrlGenerator.php | 2 | ||||
-rw-r--r-- | src/Routing/UrlGeneratorInterface.php | 13 | ||||
-rw-r--r-- | src/helpers.php | 4 |
5 files changed, 49 insertions, 4 deletions
diff --git a/src/Routing/LegacyUrlGenerator.php b/src/Routing/LegacyUrlGenerator.php new file mode 100644 index 00000000..f17bdf88 --- /dev/null +++ b/src/Routing/LegacyUrlGenerator.php @@ -0,0 +1,25 @@ +<?php + +namespace Engelsystem\Routing; + +class LegacyUrlGenerator extends UrlGenerator +{ + /** + * @param string $path + * @param array $parameters + * @return string + */ + 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); + + return $uri; + } +} diff --git a/src/Routing/RoutingServiceProvider.php b/src/Routing/RoutingServiceProvider.php index b7db1383..021840c3 100644 --- a/src/Routing/RoutingServiceProvider.php +++ b/src/Routing/RoutingServiceProvider.php @@ -8,7 +8,14 @@ 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..8dd52271 100644 --- a/src/Routing/UrlGenerator.php +++ b/src/Routing/UrlGenerator.php @@ -2,7 +2,7 @@ namespace Engelsystem\Routing; -class UrlGenerator +class UrlGenerator implements UrlGeneratorInterface { /** * @param string $path diff --git a/src/Routing/UrlGeneratorInterface.php b/src/Routing/UrlGeneratorInterface.php new file mode 100644 index 00000000..d97da30d --- /dev/null +++ b/src/Routing/UrlGeneratorInterface.php @@ -0,0 +1,13 @@ +<?php + +namespace Engelsystem\Routing; + +interface UrlGeneratorInterface +{ + /** + * @param string $path + * @param array $parameters + * @return string + */ + public function to($path, $parameters = []); +} diff --git a/src/helpers.php b/src/helpers.php index 5a48498a..1915eb86 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -5,7 +5,7 @@ use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Http\Request; use Engelsystem\Renderer\Renderer; -use Engelsystem\Routing\UrlGenerator; +use Engelsystem\Routing\UrlGeneratorInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** @@ -114,7 +114,7 @@ function session($key = null, $default = null) /** * @param string $path * @param array $parameters - * @return UrlGenerator|string + * @return UrlGeneratorInterface|string */ function url($path = null, $parameters = []) { |