Welcome Guest, Not a member yet? Register   Sign In
url encode
#1

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');
  
Simpler is always better
Reply
#2

Why do you use urlencode() for $path?
Reply
#3

(This post was last modified: 09-29-2022, 05:18 PM by donpwinston.)

(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.)
Simpler is always better
Reply
#4

Sorry, I don't get what you say.

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

(This post was last modified: 09-29-2022, 10:56 PM by donpwinston.)

(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.
Simpler is always better
Reply
#6

%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/CodeIgnit...aceholders
Reply
#7

(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/CodeIgnit...aceholders

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.
Simpler is always better
Reply
#8

(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.
Reply
#9

(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
Simpler is always better
Reply
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB