Welcome Guest, Not a member yet? Register   Sign In
Relative URLs, or Absolute URLs?
#1

[eluser]mdavis1982[/eluser]
Hi all...

I've been working with CodeIgniter for a while now, and was wondering about something that I haven't really thought about before. In my view files Ive been doing this:
Code:
<img src="&lt;?php echo base_url(); ?&gt;public/images/branding/logo.jpg" alt="Logo" />

This generates an absolute URL. What I am wondering is if there is a better way to do this so that the urls generated are relative urls to save on HTTP requests to make my site faster.

Any advice would be greatly appreciated!

Thanks...

Matt
#2

[eluser]Michael Wales[/eluser]
I would make a helper function that echos a relative (from root) URL to the image. Then when you go from development to production you only have to change that one function (rather than every image throughout the entire site).
#3

[eluser]mdavis1982[/eluser]
I'm not sure I understand what you mean. Is there any chance you could show me an example, please? It would be very much appreciated!

Cheers...

Matt
#4

[eluser]wiredesignz[/eluser]
relative URL's don't make a page load faster or lower server load, your browser simply adds the current domain to the request.
#5

[eluser]Josh Giese[/eluser]
I think he is talking about the page weight.

"http://www.somedomain.com/images/picture.jpg"
is heavier then
"/images/picture.jpg"

personally, I wouldn't really view it as a problem. most of your visitors wont feel the difference.
#6

[eluser]wiredesignz[/eluser]
Better to use BASE HREF in the header with relative urls:

&lt;BASE HREF="http://domain.com/etc"&gt;
#7

[eluser]mdavis1982[/eluser]
Josh, that's exactly what I was talking about...

I just think there should be some easy way in CI to generate relative URLs for images, etc.

Thanks all... Any more ideas would be greatly appreciated.

Matt
#8

[eluser]wiredesignz[/eluser]
application/helpers/asset_helper.php

Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

    function asset($type = 'css', $source = NULL, $attrib = NULL, $relativeURL = TRUE)
    {
        $href = ($relativeURL) ? '' : base_url();
        
        $source = $href . str_replace(".{$type}", '', $source);
        
        switch ($type)
        {            
            case 'css':
            {
                return '&lt;link rel="stylesheet" type="text/css" href="'.$source.'.'.$type.'" '.$attrib.' /&gt;'."\n";
            }
            case 'js':
            {
                return ''."\n";
            }
            case 'img':
            {
                return '<img src="'.$source.'" '.$attrib.' />'."\n";
            }
        }
    }

Useage in View

Code:
&lt;?php echo asset('css', 'css/stylesheet', 'media="screen"'); //relative URL ?&gt;

&lt;?php echo asset('img', 'images/logo.jpg', 'alt="logo"', FALSE); // full URL ?&gt;


The forum killed the javascript part of the helper? I think you get the idea though.
#9

[eluser]Phil Sturgeon[/eluser]
Yea the script tag is stripped by the XSS cleaner, so asset helpers always get broken in forums :p

Check out my asset helper for a modular sorting system too.
#10

[eluser]Sarre[/eluser]
The base_url() function just outputs the config value in config.php, so if you have a relative url in there, like
Code:
$config['base_url'] = "/some/relative_url/";
the base_url() function will output that relative url value...

What else do you need?

(Or am I mistaken? :p)




Theme © iAndrew 2016 - Forum software by © MyBB