How to generate domainless links or cache files - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: How to generate domainless links or cache files (/showthread.php?tid=26542) |
How to generate domainless links or cache files - El Forum - 01-16-2010 [eluser]Maulwurf[/eluser] By default, CI requires you to enter your base url in the config file, like: Code: $config['base_url'] = "http://localhost/myapp/"; Furthermore, to get CSS and img-tags working in your views, you need to add Code: <head> This may work well most of the times. But consider this situation: - you have a "weak" host on the web - you have a "strong" dev-pc at home which outperforms the server by ages - a killer app which requires lots of time for calculating and has little dynamic data. With CIs great caching feature, you could do like this: - calculate all files at home - load them to your server and have fabulous page load times with happy users, because mostly everything is served from cache. Unfortunately, there is one point where this approach breaks: The <base>-tag. To get your app running on your dev pc, it will be something like Code: <base href="http://localhost..."> and will never work on your server available at http://example.com But there is light at the end of the tunnel! So to get rid of the base tag, some modification is needed. Here is how I did it using CI 1.7.2: my CI setup is like this: Code: /var/www/cistuff 1. change base_url from Code: $config['base_url'] = "http://localhost/myapp/"; Code: $config['base_url'] ='/myapp/'; 2. define a new constant PATHPRE i.e. in your config/constants.php file Code: define('PATHPRE', '/cistuff/myapp/'); 3. create a new helper stringops_helper.php and add this: Code: /** as you can see, we overwrite the anchor function from CI. It always returns a complete url like "http://localhost..." which is not good to get rid of the domain name in the generated html files for transportable caching. 4. go to config/autoload.php set Code: $autoload['helper'] = array('stringops_helper', 'url'); 5. how to use it: every link you need to generate, must be run through l() or anchor() i.e. for CSS definition Code: <link type="text/css" rel="stylesheet" href="<?=l('css/style.css')?>"> i.e. for menu items Code: echo anchor(site_url('faq'), 'FAQ'); Hope it's a starting point for your own scenarios How to generate domainless links or cache files - El Forum - 01-16-2010 [eluser]CroNiX[/eluser] In my config I just use: Code: $config['base_url'] = 'http://' . $_SERVER['SERVER_NAME'] . '/'; Then all links come out fine and are generated correctly regardless of whether I'm on my dev or live server. You could be more specific for various situations too. Code: $config['base_url'] = ($_SERVER['SERVER_NAME'] == 'localhost') ? 'http://localhost/somedir/somepath/' : 'http://myrealhost.com/'; How to generate domainless links or cache files - El Forum - 01-16-2010 [eluser]Colin Williams[/eluser] I NEVER include the hostname/domain in my base_url setting, opting for the relatively absolute "/" (which was apparently your big revelation). How to generate domainless links or cache files - El Forum - 01-17-2010 [eluser]Jamie Rumbelow[/eluser] As far as dynamic URLs go, I use a little bit of code that works on all platforms and allows me to have the full domain/hostname in my base URL. It looks something like this: Code: $url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http"); How to generate domainless links or cache files - El Forum - 01-17-2010 [eluser]Colin Williams[/eluser] Your first two lines, Jamie, repeat what the browser already does when it encounters paths prefixed with a "/" That last bit is pretty useful though, since it works out subfolders. Code: $config['base_url'] = '/'. str_replace(basename($_SERVER['SCRIPT_NAME']),'',$_SERVER['SCRIPT_NAME']); How to generate domainless links or cache files - El Forum - 01-17-2010 [eluser]Jamie Rumbelow[/eluser] [quote author="Colin Williams" date="1263746846"]Your first two lines, Jamie, repeat what the browser already does when it encounters paths prefixed with a "/" That last bit is pretty useful though, since it works out subfolders. Code: $config['base_url'] = '/'. str_replace(basename($_SERVER['SCRIPT_NAME']),'',$_SERVER['SCRIPT_NAME']); I like having the full URL available (for example, when sending a validation email), and all this means is that I can have the full URL and not worry about it. Still, each man to his own. Jamie How to generate domainless links or cache files - El Forum - 01-17-2010 [eluser]Colin Williams[/eluser] Good point about the emails. Something to consider |