Welcome Guest, Not a member yet? Register   Sign In
Proper way yo deal with uri segments?
#1

(This post was last modified: 10-11-2021, 09:35 PM by dgvirtual.)

I have my development environment at the web server root (accessible through http://localhost:8080/), but my production installation is in a subdirectory of the web server root (https://site.lt/my_subdir/).

For some reason, the uri helper on the production environment returns the subdirectory name as the first element in the segment array. So I have to use funny workarounds to get the same segment in both installs, see code below. I do have proper base url settings in the .env files in each environment (app.baseURL variable points to the above-mentioned addresses)...

So, my question is: is there a better way to use the CI4 uri helper? Is there a bug in the code?

PHP Code:
$uri current_url(true);
if (
$uri->getSegment(1) == 'my_subdir'$sgm 5//bug in uri?
else $sgm 4;
if (
$uri->getSegment($sgm) == 'pdf'$variant 'pdf';
else 
$variant 'regular'
==

Donatas G.
Reply
#2

I think it’s a bug. I set up a subdomain instead of a subdirectory to avoid this problem.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

I think it will be fun https://site.lt/my_subdir/.env
Reply
#4

(This post was last modified: 10-14-2021, 12:44 AM by dgvirtual.)

(10-13-2021, 04:28 PM)iRedds Wrote: I think it will be fun  https://site.lt/my_subdir/.env

I did not use the real website name Smile, but yeh, next time I will use a non-existent domain in my examples.  And in  my server only the public folder is in the public subdirectory, the application itself, with the .env file, is two levels up in the file tree (instead of the usual 1 level), not publically accessible.

(10-12-2021, 04:05 PM)includebeer Wrote: I think it’s a bug. I set up a subdomain instead of a subdirectory to avoid this problem.
Thanks for confirming that... subdomains are harder because we can only have one SSL certificate for the site, but yeh, that would be a workaround.
==

Donatas G.
Reply
#5

(This post was last modified: 10-14-2021, 01:07 AM by John_Betong.)

Try displaying the following Server details and write your own:

PHP Code:
echo <pre>// add line feeds
  
print _r($_SERVER);
echo 
</pre>
Reply
#6

(10-14-2021, 12:42 AM)dgvirtual Wrote:
(10-13-2021, 04:28 PM)iRedds Wrote: I think it will be fun  https://site.lt/my_subdir/.env

I did not use the real website name Smile, but yeh, next time I will use a non-existent domain in my examples.  And in  my server only the public folder is in the public subdirectory, the application itself, with the .env file, is two levels up in the file tree (instead of the usual 1 level), not publically accessible.

(10-12-2021, 04:05 PM)includebeer Wrote: I think it’s a bug. I set up a subdomain instead of a subdirectory to avoid this problem.
Thanks for confirming that... subdomains are harder because we can only have one SSL certificate for the site, but yeh, that would be a workaround.

I used to have the one SSL certificate issue (and numerous outages) with my previous hosting supplier.
Then I moved to a cheaper provider (with far less issues) using CPANEL, and Let's Encrypt subdomains are automatically added.  Smile
Reply
#7

(10-14-2021, 01:06 AM)John_Betong Wrote: Try displaying the following Server details  and write your own:

echo “<pre>”; // add line feeds
  print_r($_SERVER);
echo “</pre>”;
Which values do you want to see? Here they are (on the production server, modified to hide particulars but should give you an idea):

Code:
Array
(
    [PHPRC] => /usr/local/lib/php74.ini
    [PHP_INI_SCAN_DIR] => /usr/local/lib/conf/php74/
    [PATH] => /sbin:/usr/sbin:/bin:/usr/bin
    [PWD] => /usr/local/fastcgi/cgi-bin
    [SHLVL] => 0
    [PATH_TRANSLATED] => /home/site/domains/site.lt/public_html/calls/create_complicated_test2
    [PATH_INFO] => /calls/create_complicated_test2
    [SCRIPT_NAME] => /my_subdir/index.php
    [REQUEST_URI] => /my_subdir/calls/create_complicated_test2
    [QUERY_STRING] =>
    [REQUEST_METHOD] => GET
    [SERVER_PROTOCOL] => HTTP/2.0
    [GATEWAY_INTERFACE] => CGI/1.1
    [REDIRECT_URL] => /my_subdir/index.php/calls/create_complicated_test2
    [REMOTE_PORT] => 53370
    [SCRIPT_FILENAME] => /home/site/domains/site.lt/public_html/my_subdir/index.php
    [SERVER_ADMIN] => [no address given]
    [CONTEXT_DOCUMENT_ROOT] => /usr/local/fastcgi/cgi-bin/php-cgi
    [CONTEXT_PREFIX] => /cgi-bin/php-cgi
    [REQUEST_SCHEME] => https
    [DOCUMENT_ROOT] => /home/site/domains/site.lt/public_html
    [REMOTE_ADDR] => 155.152.126.102
    [SERVER_PORT] => 443
    [SERVER_ADDR] => 145.35.53.22
    [SERVER_NAME] => site.lt
    [SERVER_SOFTWARE] => Apache
    [SERVER_SIGNATURE] =>
    [HTTP_HOST] => site.lt
    [HTTP_TE] => trailers
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_SEC_FETCH_USER] => ?1
    [HTTP_SEC_FETCH_SITE] => none
    [HTTP_SEC_FETCH_MODE] => navigate
    [HTTP_SEC_FETCH_DEST] => document
    [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
    [HTTP_COOKIE] => ci_session=4fl00psbcv8ccup3e9inh18erehpkeme
    [HTTP_ACCEPT_ENCODING] => gzip, deflate, br
    [HTTP_ACCEPT_LANGUAGE] => en,lt;q=0.8,en-US;q=0.6,ru;q=0.4,pl;q=0.2
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
    [HTTP_USER_AGENT] => Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:93.0) Gecko/20100101 Firefox/93.0
    [H2_STREAM_TAG] => 1103-15
    [H2_STREAM_ID] => 15
    [H2_PUSHED_ON] =>
    [H2_PUSHED] =>
    [H2_PUSH] => on
    [H2PUSH] => on
    [HTTP2] => on
    [SSL_TLS_SNI] => site.lt
    [HTTPS] => on
    [UNIQUE_ID] => YWf@VEyoEgMrMIrbx0JyEwwETwk
    [REDIRECT_STATUS] => 200
    [REDIRECT_HANDLER] => application/x-httpd-php
    [REDIRECT_H2_STREAM_TAG] => 1103-15
    [REDIRECT_H2_STREAM_ID] => 15
    [REDIRECT_H2_PUSHED_ON] =>
    [REDIRECT_H2_PUSHED] =>
    [REDIRECT_H2_PUSH] => on
    [REDIRECT_H2PUSH] => on
    [REDIRECT_HTTP2] => on
    [REDIRECT_SSL_TLS_SNI] => site.lt
    [REDIRECT_HTTPS] => on
    [REDIRECT_UNIQUE_ID] => YWf@VEyoEgMrMIrbx0JyEwwETwk
    [REDIRECT_REDIRECT_STATUS] => 200
    [REDIRECT_REDIRECT_H2_STREAM_TAG] => 1103-15
    [REDIRECT_REDIRECT_H2_STREAM_ID] => 15
    [REDIRECT_REDIRECT_H2_PUSHED_ON] =>
    [REDIRECT_REDIRECT_H2_PUSHED] =>
    [REDIRECT_REDIRECT_H2_PUSH] => on
    [REDIRECT_REDIRECT_H2PUSH] => on
    [REDIRECT_REDIRECT_HTTP2] => on
    [REDIRECT_REDIRECT_SSL_TLS_SNI] => site.lt
    [REDIRECT_REDIRECT_HTTPS] => on
    [REDIRECT_REDIRECT_UNIQUE_ID] => YWf@VEyoEgMrMIrbx0JyEwwETwk
    [FCGI_ROLE] => RESPONDER
    [PHP_SELF] => /my_subdir/index.php/calls/create_complicated_test2
    [REQUEST_TIME_FLOAT] => 1634205268.9127
    [REQUEST_TIME] => 1634205268
    [CI_ENVIRONMENT] => production
    [app.baseURL] => https://site.lt/my_subdir/
    [database.default.database] => ../../site/writable/site-production.sqlite3
    [database.default.DBDriver] => SQLite3
)
==

Donatas G.
Reply
#8

Compare the variables on localhost and remote that have a PATH in the name. Find the localhost path to the helper path, then modify the remote path.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB