Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: $this->request->getUri()->getPath() is not compatible with PSR-7 #7123

Closed
kenjis opened this issue Jan 15, 2023 · 10 comments
Closed

Bug: $this->request->getUri()->getPath() is not compatible with PSR-7 #7123

kenjis opened this issue Jan 15, 2023 · 10 comments
Assignees
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@kenjis
Copy link
Member

kenjis commented Jan 15, 2023

CodeIgniter4 Version: v4.3.1

$this->request->getUri()->getPath() returns the route (URI path relative to baseURL).

app.baseURL = 'http://localhost:8888/ci431/public/'

When navigating to http://localhost:8888/ci431/public/test?a=b,

$uri = $this->request->getUri();
echo (string) $uri;   // "http://localhost:8888/ci431/public/test?a=b" → Okay
echo $uri->getPath(); // "test" → NG. It should be "/ci431/public/test" when following PSR-7

Ref https://www.php-fig.org/psr/psr-7/

Related #5930

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 15, 2023
@kenjis kenjis changed the title Bug: $this->request->getUri()->getPath() returns the route Bug: $this->request->getUri()->getPath() is not compatible with PSR-7 Jan 15, 2023
@kenjis
Copy link
Member Author

kenjis commented Jan 17, 2023

I will push again for a separate class to handle internal URIs; I think we need it despite the complexity it adds.
#7124 (comment)

@kenjis
Copy link
Member Author

kenjis commented Jan 23, 2023

Navigate to http://localhost:8888/ci431/public/test?a=b:

    uri_string(): test
uri_string(true): test

               current_url(): http://localhost:8888/ci431/public/index.php/test
current_url(true)->getPath(): /ci431/public/index.php/test
  (string) current_url(true): http://localhost:8888/ci431/public/index.php/test?a=b

  (string) $this->request->getUri(): http://localhost:8888/ci431/public/test?a=b
$this->request->getUri()->getPath(): test

@kenjis
Copy link
Member Author

kenjis commented Feb 10, 2023

Navigate to http://localhost:8888/ci431/public/index.php/test?a=b:

    uri_string(): test
uri_string(true): test

               current_url(): http://localhost:8888/ci431/public/index.php/test
current_url(true)->getPath(): /ci431/public/index.php/test
  (string) current_url(true): http://localhost:8888/ci431/public/index.php/test?a=b

  (string) $this->request->getUri(): http://localhost:8888/ci431/public/test?a=b
$this->request->getUri()->getPath(): test

@kenjis
Copy link
Member Author

kenjis commented Feb 11, 2023

current_url() returns the URL with index.php.
But $this->request->getUri() does not have index.php.

I think when current_url() has index.php, the URI object also should have index.php.

@kenjis
Copy link
Member Author

kenjis commented Feb 11, 2023

From #7233 (comment)

The inconsistency of URI Segments between $this->request->getUri() and current_url(true).

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        var_dump($this->request->getUri()->getSegments());
        var_dump($this->request->getUri()->getSegment(1));
        $uri = current_url(true);
        var_dump($uri->getSegments());
        var_dump($uri->getSegment(1));
    }
}
/Applications/MAMP/htdocs/ci431/app/Controllers/Home.php:9:
array (size=3)
  0 => string 'one' (length=3)
  1 => string 'two' (length=3)
  2 => string 'three' (length=5)

/Applications/MAMP/htdocs/ci431/app/Controllers/Home.php:10:string 'one' (length=3)

/Applications/MAMP/htdocs/ci431/app/Controllers/Home.php:12:
array (size=6)
  0 => string 'ci431' (length=5)
  1 => string 'public' (length=6)
  2 => string 'index.php' (length=9)
  3 => string 'one' (length=3)
  4 => string 'two' (length=3)
  5 => string 'three' (length=5)

/Applications/MAMP/htdocs/ci431/app/Controllers/Home.php:13:string 'ci431' (length=5)

@kenjis kenjis self-assigned this Feb 13, 2023
@kenjis kenjis mentioned this issue Feb 14, 2023
5 tasks
@kenjis
Copy link
Member Author

kenjis commented Feb 18, 2023

About PSR-7 URI::getPath(): #7252 (comment)

@kenjis
Copy link
Member Author

kenjis commented Feb 20, 2023

Navigate to http://localhost:8888/ci431/public/:

    uri_string(): /
uri_string(true): 

               current_url(): http://localhost:8888/ci431/public/index.php/
current_url(true)->getPath(): /ci431/public/index.php/
  (string) current_url(true): http://localhost:8888/ci431/public/index.php/

  (string) $this->request->getUri(): http://localhost:8888/ci431/public/
$this->request->getUri()->getPath(): /

@kenjis
Copy link
Member Author

kenjis commented Feb 20, 2023

Navigate to http://localhost:8888/ci431/public/index.php or http://localhost:8888/ci431/public/index.php/:

    uri_string(): /
uri_string(true): 

               current_url(): http://localhost:8888/ci431/public/index.php/
current_url(true)->getPath(): /ci431/public/index.php/
  (string) current_url(true): http://localhost:8888/ci431/public/index.php/

  (string) $this->request->getUri(): http://localhost:8888/ci431/public/
$this->request->getUri()->getPath(): /

@kenjis
Copy link
Member Author

kenjis commented Feb 24, 2023

Finally, I think URI and SiteURL can be made fully compatible (and having some extended functions) with PSR-7.
#7252 (comment)

I wrote above, but...
See #7252 (comment)
It seems if we make our URI PSR-7 compatible, we need to distinguish the path /foo/bar and foo/bar.
When the baseURL is http://localhost/ci4/, $uri->withPath('/foo/bar') will be http://localhost/foo/bar.
$uri->withPath('foo/bar') will be http://localhost/ci4/foo/bar.

But we do not distinguish the route path /foo/bar and foo/bar.
They are exactly the same.

It seems difficult to make our URI fully compliant with PSR-7.

@kenjis
Copy link
Member Author

kenjis commented Aug 17, 2023

Closed by #7282

@kenjis kenjis closed this as completed Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

1 participant