Comments (13)
For the records (I found this issue because I wanted to solve the same problem), another solution (which I consider better, since it prevents duplicate content), with Zend Diactoros:
use Zend\Diactoros\Response\RedirectResponse;
$router->get('/{any}/', function (ServerRequestInterface $request, $any) {
$redirect = rtrim($request->getUri(), '/');
return new RedirectResponse($redirect, 301);
});
It can be adapted to add a trailing slash if you prefer. I think the route should then be added last.
from route.
Route won't handle both by default as you shouldn't as you shouldn't, it
causes inconsistencies and can lead to Google seeing duplicate content,
however, if you really want to do that it's very easy to do so in your
bootstrap.
On 12 Sep 2015 18:59, "Khayrattee Wasseem" [email protected] wrote:
By default, trailing slashes are not handled - can this be IN by
default please?Example:
/page and /page/ should both be OK if only /page was added in
RouteCollection—
Reply to this email directly or view it on GitHub
#70.
from route.
"Google seeing it as a duplicate content" is not an acceptable answer please. Handling duplicate content forms part of a strategy, namely an "seo strategy", and not the task of "coding framework", example using rel="canonical" (REF: https://support.google.com/webmasters/answer/139066?hl=en)
Other frameworks have it, so I don't see why we can't have it "by default" - give it another thought ;)
Keep up the good work!
from route.
I'm sorry by it won't be handled by default.
As this is not a framework but a tool we can't make assumptions about what
is being built, however, as I say, it can easily be implemented with a
quick trim of the path on the request.
On 12 Sep 2015 19:39, "Khayrattee Wasseem" [email protected] wrote:
"Google seeing it as a duplicate content" is not an acceptable answer
please. Handling duplicate content forms part of a strategy, namely an "seo
strategy", and not the task of "coding framework", example using
rel="canonical" (REF:
https://support.google.com/webmasters/answer/139066?hl=en)Other frameworks have it, so I don't see why we can't have it "by default"
- give it another thought ;)
Keep up the good work!
—
Reply to this email directly or view it on GitHub
#70 (comment).
from route.
sure, fair enough - noted.
Can you confirm if this trailing process looks good enough:
$pathInfo = $request->getPathInfo();
$replacement_count = 1;
$pathInfo = str_replace('/?', '?', $pathInfo, $replacement_count);
$pathInfo = rtrim($pathInfo, '/');
//and then, something like:
$response = $dispatcher->dispatch($request->getMethod(), $pathInfo);
from route.
If you register all of your routes without a trailing slash then you can simply do the following as the ?
should never be in the path info anyway.
$pathInfo = rtrim($request->getPathInfo(), '/');
// ...
$response = $dispatcher->dispatch($request->getMethod(), $pathInfo);
It will change with v2 but I will document it.
from route.
yes correct, I've just tested this.
Btw when you say change, what kind of them? :) Hope it's not a very big change.. Yes please do document.
thanks.
from route.
Well its PSR-7 compatible so it will just accept a request and a response
to dispatch, this way its a compatible middleware too.
On 12 Sep 2015 21:12, "Khayrattee Wasseem" [email protected] wrote:
yes correct, I've just tested this.
Btw when you say change, what kind of them? :) Hope it's not a very big
change.. Yes please do document.
thanks.—
Reply to this email directly or view it on GitHub
#70 (comment).
from route.
A way todo this in v2:
$container->share('request', function () {
$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
return $request->withUri($request->getUri()->withPath(rtrim($request->getUri()->getPath(), '/')));
});
or
$container->share('request', function () {
$_SERVER['REQUEST_URI'] = rtrim($_SERVER['REQUEST_URI'], '/');
return Zend\Diactoros\ServerRequestFactory::fromGlobals();
});
from route.
Hi Maarten,
You can use Zend\Diactoros\ServerRequestFactory::fromGlobals();
, no need to pass them.
from route.
I see, thanks Hannes!
Updated the examples.
from route.
Just to be clear, Route no longer allows direct passing of the path info. But there is no need to implement a code solution to this, you can simply define one or the other and have your webserver force or strip the trailing slash.
from route.
Yeah I was still thinking with PHP built-in server... I guess I need some sleep.
from route.
Related Issues (20)
- Feature request: custom attributes for route and route group accessible within a middleware HOT 6
- Catch Exception Best Way HOT 3
- Feature request: map with array of methods HOT 1
- Feature request: make $request available in getOptionsCallable HOT 2
- I cannot make post requests HOT 5
- Url resolve fails with 2 numeric parameter HOT 3
- Add an option to get the target route from the middleware HOT 2
- Why isn't the middleware stack executed for unmatched routes? HOT 2
- psr/simple-cache version constraints HOT 2
- Allow for empty route paths to be treat as the base route path HOT 2
- Nested groups are required HOT 1
- Feature request: Treat Route `$handler` that implements RequestHandlerInterface as callable
- Root exception class (and possibly, its children) not compatible with PHP7+
- Request matcher? guess if ServerRequest can be handled
- Dispatcher: handle and find HOT 1
- Feature: Extra map() options
- Method to get declared routes?
- How can I assign controller to errors http ? HOT 1
- Cannot Change Cache Key
- Is this package still actively maintained? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from route.