• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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

#2
[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/';

#3
[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).

#4
[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");
$url .= "://".$_SERVER['HTTP_HOST'];
$url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

$config['base_url']    = $url;

#5
[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']);

#6
[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']);
[/quote]

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

#7
[eluser]Colin Williams[/eluser]
Good point about the emails. Something to consider


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.