Welcome Guest, Not a member yet? Register   Sign In
How to generate domainless links or cache files
#1

[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>
    <base href="<?= base_url().APPPATH ?>" />
        ...
</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  
/var/www/cistuff/ci
/var/www/cistuff/myapp1
/var/www/cistuff/myapp1.php
/var/www/cistuff/myapp2
/var/www/cistuff/myapp2.php
this way I can keep the Framework separated from the apps


1. change base_url
from
Code:
$config['base_url']    = "http://localhost/myapp/";
to
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:
/**
* creates link paths
* link helper to get rid of base tag in basepage
*/
function l($link)
{
  return PATHPRE.$link;
}

/**
* overwrite anchor function
*/
function anchor($link, $title)
{
  return '<a href="'.($link).'">'.$title.'</a>';
}

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');
//stringops before url!!
stringops must be loaded before url, so we can overwrite the anchor function. Otherwise you will get "cannot redeclare anchor(), previously declared in ..."


5. how to use it:
every link you need to generate, must be run through l() or anchor()

i.e. for CSS definition
Code:
&lt;link type="text/css" rel="stylesheet" href="&lt;?=l('css/style.css')?&gt;"&gt;

i.e. for menu items
Code:
echo anchor(site_url('faq'), 'FAQ');

Hope it's a starting point for your own scenarios Wink


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



Theme © iAndrew 2016 - Forum software by © MyBB