How to generate domainless links or cache files |
[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 |
Messages In This Thread |
How to generate domainless links or cache files - by El Forum - 01-16-2010, 12:56 PM
How to generate domainless links or cache files - by El Forum - 01-16-2010, 04:20 PM
How to generate domainless links or cache files - by El Forum - 01-16-2010, 10:32 PM
How to generate domainless links or cache files - by El Forum - 01-17-2010, 03:02 AM
How to generate domainless links or cache files - by El Forum - 01-17-2010, 04:47 AM
How to generate domainless links or cache files - by El Forum - 01-17-2010, 04:50 AM
How to generate domainless links or cache files - by El Forum - 01-17-2010, 04:54 AM
|