CodeIgniter Forums
Getting feet wet-- couple of "best practice" questions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Getting feet wet-- couple of "best practice" questions (/showthread.php?tid=29395)

Pages: 1 2


Getting feet wet-- couple of "best practice" questions - El Forum - 04-08-2010

[eluser]Fumigator[/eluser]
Hi there, I'm just getting into CI and am enjoying it so far. Initially I had some trouble with how paths are set up, but some searches of this forum and the user guide have cleared most of it up.

So now, I'm wondering: How do most people set up asset paths? Is there a "best way"? For now I've created folders in the root as siblings to CI's "system" folder, and that works OK, as long as I use base_url() when pointing to assets. But I do admit, it kind of bugs me to use an absolute path; I've always used relative paths because as we all know, you don't want to break all of your links if environments change. (Of course, in the past, using a relative path meant I _had_ to had my "img" folder as a child right under my root folder, so that's not exactly flexible, so perhaps using base_url() is an improvement!)

Next, I love using jQuery's AJAX functions. Before CI, my AJAX calls refer to my PHP files using relative path. Now though, I need to get the value returned by base_url() into my Javascript script I suppose. I've thought I can stick a < script > code block inside my view, which loads values into global Javascript variables that contain the paths to my PHP files, and then I can use these global variables in my Javascript. Is this "best practice", or is there a better way?


Getting feet wet-- couple of "best practice" questions - El Forum - 04-08-2010

[eluser]Jelmer[/eluser]
Just a short suggestion: search the forums for asset management libraries and take your ideas from those - people writing such libraries have generally thought pretty good about it Wink.

Ocular springs to mind as I took my own asset management cues from that library, but there are a gazillion others - in the wiki as well as in the forums.

EDIT: and to your Ajax/jQuery question, you could look into the CodeIgniter 2 code if there's something in the new JavaScript library that might help with those - I don't know as I haven't looked at those myself.


Getting feet wet-- couple of "best practice" questions - El Forum - 04-08-2010

[eluser]pickupman[/eluser]
I use jQuery quit a bit as well, and Jelmer mentioned there are some good libraries for assests. I personally like using Carabiner. Helps tidy up scripts and css. When using ajax call in jQuery I would use something like:
Code:
//script block
jQuery('<?php echo site_url("controller/method");?>',{vars: var},function(response){
alert(response); //Something cool here
});



Getting feet wet-- couple of "best practice" questions - El Forum - 04-09-2010

[eluser]mddd[/eluser]
I think site_url is neater than base_url. Because there could be situations where they will be different. Using site_url(...) gives more flexbility than only echoing base_url and then putting the rest of the url straight in your html.


Getting feet wet-- couple of "best practice" questions - El Forum - 04-09-2010

[eluser]pickupman[/eluser]
site_url function also still works even if you enable or disable having index.php in your url.
Say you have:
http://www.example.com/index.php/controller/method

And switch to:
http://www.example.com/controller/method

site_url takes care of both without changing any code. base_url is good for setting paths to assets.


Getting feet wet-- couple of "best practice" questions - El Forum - 04-09-2010

[eluser]mikelbring[/eluser]
I dont really use that method to set assets because you need to not use a full url to images and style sheets. It needs to be /assets/image.jpg instead of domain.com/assets/image.jpg. It creates a http request for every duplicate image when you do it with the domain. I actually set a constant in t he index file called like BINPATH and just put the path in there. (mine are called bin instead of assets).


Getting feet wet-- couple of "best practice" questions - El Forum - 04-12-2010

[eluser]Fumigator[/eluser]
[quote author="mikelbring" date="1270838934"]I dont really use that method to set assets because you need to not use a full url to images and style sheets. It needs to be /assets/image.jpg instead of domain.com/assets/image.jpg. It creates a http request for every duplicate image when you do it with the domain. I actually set a constant in t he index file called like BINPATH and just put the path in there. (mine are called bin instead of assets).[/quote]

Yeah... this was something that was bothering me, why would I want a URL reference to every image? So I've taken your advice and added a constant that I can use to create a relative reference to my images. Thanks.


Getting feet wet-- couple of "best practice" questions - El Forum - 04-12-2010

[eluser]cahva[/eluser]
[quote author="Fumigator" date="1271121420"]
Yeah... this was something that was bothering me, why would I want a URL reference to every image? So I've taken your advice and added a constant that I can use to create a relative reference to my images. Thanks.[/quote]

..or just change your $config['base_url'] Smile

I have this set up in almost every project and have no problem at all:
Code:
$config['base_url']    = "/";
That way you can use site_url, anchor etc. everywhere. As it is pointed out, browser will know in which domain you are on Smile

Ofcourse full url is sometimes needed(for example payment methods which want return url) and therefore I add something like $config['full_url'] = 'http://www.somedomain.tld/' which can be then grabbed with config_item('full_url') when I need it somewhere.


Getting feet wet-- couple of "best practice" questions - El Forum - 04-12-2010

[eluser]mikelbring[/eluser]
I may be wrong, but doing that means you need to be on a sub/domain and your app has to be in the root? I do a lot of projects on localhost before I release them.


Getting feet wet-- couple of "best practice" questions - El Forum - 04-13-2010

[eluser]cahva[/eluser]
What stops you putting the directory to that? It doesnt matter what domain you are using, if its on localhost http://localhost/mynewproject/, you would put "/mynewproject/" as baseurl.