Last active
August 29, 2015 14:05
-
-
Save mamor/61269596ded5001c4e1f to your computer and use it in GitHub Desktop.
trailing-slash in URLs on Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/config/app.php b/app/config/app.php | |
index 10ae94b..f7a798f 100644 | |
--- a/app/config/app.php | |
+++ b/app/config/app.php | |
@@ -121,6 +121,7 @@ return array( | |
'Illuminate\Validation\ValidationServiceProvider', | |
'Illuminate\View\ViewServiceProvider', | |
'Illuminate\Workbench\WorkbenchServiceProvider', | |
+ 'App\ServiceProvider', | |
), | |
diff --git a/composer.json b/composer.json | |
index 4f00769..d6ffe20 100644 | |
--- a/composer.json | |
+++ b/composer.json | |
@@ -14,7 +14,10 @@ | |
"app/database/migrations", | |
"app/database/seeds", | |
"app/tests/TestCase.php" | |
- ] | |
+ ], | |
+ "psr-4": { | |
+ "App\\": "src/" | |
+ } | |
}, | |
"scripts": { | |
"post-install-cmd": [ | |
diff --git a/public/.htaccess b/public/.htaccess | |
index 77827ae..49221a4 100644 | |
--- a/public/.htaccess | |
+++ b/public/.htaccess | |
@@ -6,7 +6,9 @@ | |
RewriteEngine On | |
# Redirect Trailing Slashes... | |
- RewriteRule ^(.*)/$ /$1 [L,R=301] | |
+ RewriteCond %{REQUEST_URI} !/$ | |
+ RewriteCond %{REQUEST_URI} !\.[^/\.]+$ | |
+ RewriteRule .* %{REQUEST_URI}/ [L,R=301] | |
# Handle Front Controller... | |
RewriteCond %{REQUEST_FILENAME} !-d | |
diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php | |
new file mode 100644 | |
index 0000000..8f78543 | |
--- /dev/null | |
+++ b/src/ServiceProvider.php | |
@@ -0,0 +1,25 @@ | |
+<?php namespace App; | |
+ | |
+class ServiceProvider extends \Illuminate\Support\ServiceProvider | |
+{ | |
+ public function register() | |
+ { | |
+ $this->registerUrlGenerator(); | |
+ } | |
+ | |
+ /** | |
+ * @see \Illuminate\Routing\RoutingServiceProvider | |
+ */ | |
+ protected function registerUrlGenerator() | |
+ { | |
+ $this->app['url'] = $this->app->share(function($app) | |
+ { | |
+ $routes = $app['router']->getRoutes(); | |
+ | |
+ return new UrlGenerator($routes, $app->rebinding('request', function($app, $request) | |
+ { | |
+ $app['url']->setRequest($request); | |
+ })); | |
+ }); | |
+ } | |
+} | |
diff --git a/src/UrlGenerator.php b/src/UrlGenerator.php | |
new file mode 100644 | |
index 0000000..6da2a2c | |
--- /dev/null | |
+++ b/src/UrlGenerator.php | |
@@ -0,0 +1,15 @@ | |
+<?php namespace App; | |
+ | |
+class UrlGenerator extends \Illuminate\Routing\UrlGenerator | |
+{ | |
+ protected function trimUrl($root, $path, $tail = '') | |
+ { | |
+ $url = parent::trimUrl($root, $path, $tail); | |
+ | |
+ if (rtrim($root, '/') === rtrim($url, '/')) { | |
+ return rtrim($url, '/').'/'; | |
+ } | |
+ | |
+ return preg_match('/\.[^\/\.]+\z/', $url) ? $url : $url.'/'; | |
+ } | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment