Welcome Guest, Not a member yet? Register   Sign In
use relative url instead of full: https://example.com/route/item -> /route/item
#1

Hi there, 
is there a way to use a relative url's instead of full url which includes protocol, host, ...
eg:
PHP Code:
echo base_url'project/route/item' ); // to produce "/project/route/item", instead of "https://example.com/project/route/item" 
One of the reason behind this need is that full url creates a lots of problems while developing site from remote machine using ssh port forwarding.
Reply
#2

These should help you out.

CodeIgniter 4 User Guide - url_to()

CodeIgniter 4 User Guide - route_to()

CodeIgniter 4 User Guide - Named Routes
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thank you for reply,



but my question was not about the generating a link, but about the way link is generated. If i replace all links with a static once, the codeigniter still wont work correctly and force me jump to the url with prefix defined in a app.baseURL. All of the helper functions and codeigniter core itself will provide links with app.baseURL in prefix. But I cant specify, let  say

PHP Code:
$baseURL ="/project/"
,which will throw "'/project/' is not a valid URL" exception.



To make it short, I would like to see relative path generated by all those functions (base_url, url_to, .. etc ) and codeigniter core itself rather then full url path to all generated links.

Code:
~ ~ ~  8<  App.php >8   ~ ~ ~
public string $baseURL = '/project/';
~ ~ ~  8<  App.php >8   ~ ~ ~

~ ~ ~  8<  myview.php >8   ~ ~ ~
<a href="<?= base_url( "route/user" ) ?>">user</a>  
<a href="<?= url_to( "User::profile" ) ?>">profile</a>  
~ ~ ~  8<  myview.php >8   ~ ~ ~

~ ~ ~  8<  web - I would love to see >8   ~ ~ ~
<a href="/project/route/user">user</a>  
<a href="/project/route/user/profile">profile</a>  
~ ~ ~  8<  web >8   ~ ~ ~

~ ~ ~  8<  web  - what I actually see >8   ~ ~ ~
<a href="https://example.com/project/route/user">user</a>  
<a href="https://example.com/project/route/user/profile">profile</a>  
~ ~ ~  8<  web >8   ~ ~ ~

How to get rid of "https://example.com".

Thanks.
Reply
#4

There is no such a way.
You need to customize the functions/framework.

These are basic functions that are used in the framework,
so it is better to add new functions to return relative path values,
and use them in your app.
Reply
#5

@kenjis  Thanks for reply.

That is quite sad, my function will not override codeigniter core development url generating things...
Imho, will be nice if codeigniter adds config options for generation url, something like : full url, no protocol (http/https), no host/port
Reply
#6

@nerozero It is strange that base_url() returns relative paths. It is a function for returning the base URL.

If you want

PHP Code:
echo base_url('project/route/item'); 

to produce "/project/route/item",
why don't you write it as following?

PHP Code:
echo '/project/route/item'
Reply
#7

@kenjis
Here is some of the situation,
  1. Developing, the website is not always accessed from the same machine on which website is constructed.
  2. Production, when website is not on the website root directory, which is not strictly known at the moment of development or it may change in the future.
  3. Production, when website is behind http proxy
  4. Production, website has 2 different urls for application, full and short, eg: "https://example.com/accounting" and "https://example.com/acc", or even "https://acc.example.com/".... 
Most of them can be solved by own url generation, but all the nice development features will not properly work due to built-in framework url generation.
If there is already recommended approach to generate result url, such as "url_to(), base_url(), route_to()...", will be nice to have some flexibility built-in into framework.
From my perspective a better approach will be - having config variables like "$web_root" and "$allowed_hosts = [ "example.com", "example2.com","www.example2.com"]
Reply
#8

Maybe if you create a function to get just the path from base_url(), it seems like it would meet your needs.

CodeIgniter is an open source framework, so you could contribute the new function.
Reply
#9

@kenjis good point. This is a 2nd week I have start working with codeigniter, I need to understand it deeper to contribute anything useful.

Thanks for quick replies.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB