Coder Social home page Coder Social logo

Comments (13)

Arcesilas avatar Arcesilas commented on July 3, 2024 2

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.

philipobenito avatar philipobenito commented on July 3, 2024

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.

wkhayrattee avatar wkhayrattee commented on July 3, 2024

"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.

philipobenito avatar philipobenito commented on July 3, 2024

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.

wkhayrattee avatar wkhayrattee commented on July 3, 2024

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.

philipobenito avatar philipobenito commented on July 3, 2024

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.

wkhayrattee avatar wkhayrattee commented on July 3, 2024

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.

philipobenito avatar philipobenito commented on July 3, 2024

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.

maartenscholz avatar maartenscholz commented on July 3, 2024

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.

hannesvdvreken avatar hannesvdvreken commented on July 3, 2024

Hi Maarten,

You can use Zend\Diactoros\ServerRequestFactory::fromGlobals();, no need to pass them.

from route.

maartenscholz avatar maartenscholz commented on July 3, 2024

I see, thanks Hannes!

Updated the examples.

from route.

philipobenito avatar philipobenito commented on July 3, 2024

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.

Arcesilas avatar Arcesilas commented on July 3, 2024

Yeah I was still thinking with PHP built-in server... I guess I need some sleep.

from route.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.