CodeIgniter Forums
url encode - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: url encode (/showthread.php?tid=83465)

Pages: 1 2


url encode - donpwinston - 09-28-2022

I've a controller below. When I redirect the parameter is getting decoded and CodeIgniter can't match the URL and I get a 404. What's going on here?
My route is:
PHP Code:
$routes->get('/operations/path-item/(:segment)''Operations::path_item/$1'); 

PHP Code:
public function data()
  {
    $path $this->request->getGet('path');
    if ($path)
      return redirect()->to(base_url("/operations/path-item/' . urlencode($path")));

    $download $this->request->getGet('download');
    if ($download)
      return redirect()->to(base_url('/operations/download-item/' urlencode($download)));

    print view('header', [
      'title' => 'Data Directory',
      'meta_desc' => self::META_DESC,
    ]);

    print view('Operations/data', [
      'env' => getenv('BNCENV'),
      'today' => date('md'),
    ]);

    print view('footer');
  



RE: url encode - kenjis - 09-28-2022

Why do you use urlencode() for $path?


RE: url encode - donpwinston - 09-29-2022

(09-28-2022, 08:39 PM)kenjis Wrote: Why do you use urlencode() for $path?

Because path will equal something like '/data/reports/some_file.txt'

so <a href="/operations/path-item/<?= $path ?>">click me</a>

will result in <a href="/operations/path-item//data/reports/some_file.txt">click me</a>

(It appears in some cases that CodeIgniter url decodes and url encodes html form data for you. I'm not sure if I like that.)


RE: url encode - kenjis - 09-29-2022

Sorry, I don't get what you say.

In my opinion, the code should be:
Code:
base_url("/operations/path-item/$path")



RE: url encode - donpwinston - 09-29-2022

(09-29-2022, 06:54 PM)kenjis Wrote: Sorry, I don't get what you say.

In my opinion, the code should be:
Code:
base_url("/operations/path-item/$path")

The $path var = '/data/reports/some_file.txt' therefore the url will = '/operations/path-item//data/reports/some_file.txt' and the url will not work.

Need to encode $path so url will = '/operations/path-item/%2Fdata%2Freports...' then this will work.


RE: url encode - kenjis - 09-29-2022

%2F equals /
/operations/path-item//data/reports/some_file.txt and /operations/path-item/%2Fdata%2Freports%2Fsome_file.txt
are the same.

If you want to capture a part of a URI that has slashes, you need to use (:any):
https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#placeholders


RE: url encode - donpwinston - 09-30-2022

(09-29-2022, 11:32 PM)kenjis Wrote: %2F equals /
/operations/path-item//data/reports/some_file.txt and /operations/path-item/%2Fdata%2Freports%2Fsome_file.txt
are the same.

If you want to capture a part of a URI that has slashes, you need to use (:any):
https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#placeholders

No. 
:any allows an arbitrary number of segments. That's not what I want. I need '/data/report/some_file.txt' to be one segment. To do so I need to url encode it. CodeIgniter is decoding it for some reason.


RE: url encode - kenjis - 09-30-2022

(09-30-2022, 09:24 AM)donpwinston Wrote: No. 
:any allows an arbitrary number of segments. That's not what I want. I need '/data/report/some_file.txt' to be one segment. To do so I need to url encode it. CodeIgniter is decoding it for some reason.

'/data/report/some_file.txt' is not a segment, but three segments.

The solutions I can come up with are:

1. Use query string, not URI segment
2. Use another encoding method that is safe as URI, not URL encoding

CI4 probably encodes/decodes URI string for normalizing the URI.


RE: url encode - donpwinston - 10-01-2022

(09-30-2022, 02:08 PM)kenjis Wrote:
(09-30-2022, 09:24 AM)donpwinston Wrote: No. 
:any allows an arbitrary number of segments. That's not what I want. I need '/data/report/some_file.txt' to be one segment. To do so I need to url encode it. CodeIgniter is decoding it for some reason.

'/data/report/some_file.txt' is not a segment, but three segments.

The solutions I can come up with are:

1. Use query string, not URI segment
2. Use another encoding method that is safe as URI, not URL encoding

CI4 probably encodes/decodes URI string for normalizing the URI.
Yes, I've resorted to using ?path=/data/reports/some_file.txt


RE: url encode - heriniaina - 10-02-2022

Just try
PHP Code:
redirect()->to("/operations/path-item/' . $path);