I have been using CodeIgniter Framework for some months.
In views, I usually include external css and js with base_url(), just like this:
<link href="<?php echo base_url() ?>assets/css/custom.css" rel="stylesheet">
But someone tells me that using ./ also works too. And that's true.
So, what is better ( in security )?
base_url() is a better choice to use.
There isn't a cost difference using a relative or complete url, and this way you know it will point to the right spot.
It's a better habit to be in, and you can pass URL parameters to it and keep code neat.
base_url() is just a helper (URL helper in CI). Some people just find it easier to use, so I don't see any added security by using base_url().
In case of ease of use, I see so many people use it like
<?php echo base_url()."controller/function";?>
while actually you can use it like
<?php echo base_url("controller/function");?>
You can used either, but its a good practice in Codeigniter to use base_url().
you can use use ./ instead of base_url(). ./ is the root directory of your codeIgniter installation and it will get that bath in most cases that are same as base_url(), but base_url() is best practice to use because in some server ./ does not work or some other issue.
Related
So, I am working on a website with backend for two customers. Both will have different URLs. I had a problem with Javascript links (ajax calls using url:) but that was solved using a global :
var SiteURL='<?php echo base_url();?>';
and append it to calls as necessary. The problem is when my paths go deeper e.g. website.com/book/sheet/2
the relative links would continue from 2/
While I am done with the JS, I am really lost on the CSS. I don't want to give absolute path, e.g. for background-image:url() as it will change with new customers.
Any way I can make use of base_url() or any other function?
#DFriend: that somehow failed for mine as it looked for images folder in css...Firebug showed "http://localhost/samplestud/assets/css/images/listicons/arrow_double.png"
Simple "../images" seems to have fixed it pretty well. Thanks fellas.
i was given a pretty big cakePHP (built on v. 1.3.10) project to maintain. The problem is that the majority of the paths are absolute (which on my opinion is a bad habit).
Eg. in default.ctp there is:
<link rel="stylesheet" href="/css/public_new.css" />
but then at the bottom of the same file there is:
<?php echo $html->script('jquery-ui-1.8.16.custom.min.js'); ?>
which prints the correct paths.
It's like the original developers made the site to put in a server's root (not in a subdirectory).
Things ive tried to solve this problem without success:
modified the .htaccess files on /, app/, and app/webroot
adding a tag
I know i can add a $this->base to the beginning of every path, but this is not a solution since there are thousands of files to modifiy :(
So my question: is there any solution using mod_rewrite or such?
Thanks in advance.
$html->script() will automatically prepend /js/ in the HTML output. Cake's .htaccess will then point these file calls to /webroot/js/.
So <?php echo $html->script('jquery-ui-1.8.16.custom.min.js'); ?> would output
/js/jquery-ui-1.8.16.custom.min.js in the HTML.
To replace all those urls / links you could actually use a program like Actual Search and Replace ;-) I use it a lot.
Solution:
on app/config/boostrap.php, add:
Configure::write('App.base', '/teka_new/');
I take this out from Ho do I change the base path of routes in CakePHP?
If I use relative paths, like this:
<script src="../../../public/js/jquery-1.8.3.min.js"></script>
They don't work with Laravel!
When I use Root Relative Paths like this:
<script src="/sis_colgate/public/js/jquery-1.8.3.min.js"></script>
Everything works fine but if I have to change location, I have to modify all of them!
If I use Laravel URL::base()
<?php echo'<script src="'.URL::base().'/js/jquery-1.8.3.min.js"></script>'; ?>
It works fine but on client-side (Dreamweaver) I can't see the images or the links for my scripts.
Is there any other way to do this?
Thanks!
The most correct option is to use the URL::base().
But you should not use dreamweaver to see your front end.
You can use it to write the code, but remember that dreamweaver is not a browser and it will never be.
To see the result of your code you have to setup a apache server or upload the code to a server.
You can use URL::to_asset('js/jquery-1.8.3.min.js'); in Laravel 3 and URL::asset('js/jquery-1.8.3.min.js'); in Laravel 4.
The problem with relative URL's is it depends on what your current location is. You should consider using URL::to_asset() which is the best choice for your current situtation. It uses absolute path and the base path is configurable just incase you plan on hosting your assets elsewhere (say, amazon S3/Cloudfront).
Dear all
One could also do this in Laravel 4:
<script src="<?php echo asset('js/main'); ?>"></script>
or the blade templating equivalent
<script src="{{ asset('js/main.js') }}"></script>
Have a good one!
Dreamweaver allows you to set a base folder in its preferences, so you should be able to use absolute paths. Generally your base folder should be at the /sis_colgate/public/ directory, not anything above it. That way it doesn't depend on the directory structure of your current computer.
I use CodeIgniter and i love it, but i don't know whether it's really worth it to do like this:
<link rel="stylesheet" href="<?php echo base_url(); ?>css/main.css" />
<script src="<?php echo base_url(); ?>js/jquery.js"></script>
<script src="<?php echo base_url(); ?>js/functions.js"></script>
...
<img src="<?php echo base_url(); ?>images/dolphin.png" />
Rather than just:
<link rel="stylesheet" href="/css/main.css" />
<script src="/js/jquery.js"></script>
<script src="/js/functions.js"></script>
...
<img src="/images/dolphin.png" />
The first method adds a lot of weight to the page but it's reliable when you decide to use the same app in a subfolder and such.
Which one should i go with?
If you think you might need to move the app to other subfolders (and not other sub domains), it probably is worth using <?php echo base_url(); ?>, however if you can assume that the app will always be installed on it's own domain or sub domain definitely do away with the function call, it adds unnecessary clutter and sends more to the users browser.
It's down to what you think your application will need to do.
In my opinion you should always use base_url() for defining your path because like that you will make sure that your path will always be the right one. If you think that echo function is too dirty, you can always use Template engines like Code igniter built in template parser class or some external like Smarty.
Just thought I'd add some things that has been relevant for me.
As you've mentioned, if you ever need to install Codeigniter in a subdirectory, the / leading forward slash will of course not work. You would have to include the subdirectory name in the path. Personally, this comes up a lot because we'll install redesigns or prototypes in sub-directories. However, this can be nice (if it's applicable) for switching to SSL if you aren't already letting CI auto-detect your base url (as of v2.0.2).
Changing your $config['base_url'] to not include the full domain can be a bad idea. Off the top of my head, this would break links and references in emails sent by your application that use the base_url() function, and in general is likely to cause unexpected results.
Almost every HTML tag you need to use your base url in, like <link> <img> and <a>, is covered by a Codeigniter function. (link_tag(), anchor(), img()). These will take care of the base url for you. (Why they left out <script> is beyond me...)
However, I agree - using the full base url adds a lot of unnecessary page weight, especially in your navigation. Here's what I do to grab the path (in case of sub-directory installs):
// constants.php
$base_uri = $_SERVER['SCRIPT_NAME']; // Always index.php (bootstrap), right?
$base_uri = str_replace('index.php', '', $base_uri);
define('BASE_URI', $base_uri);
You can change this to a function or config item or whatever, I prefer a constant. Then you can use:
<script src="<?php echo BASE_URI; ?>js/functions.js"></script>
This will usually be a long-winded way to say /, but handles the sub-directory issue.
This might seem like a waste of time, but when you have lots of installations using the same codebase, less configuration is better.
As a compromise between readability and flexibility I would do the following:
<link rel="stylesheet" href="<?= site_url('css/main.css') ?>" />
<script src="<?= site_url('js/jquery.js); ?>"></script>
<script src="<?= site_url('js/functions.js); ?>"></script>
...
<img src="<?= site_url('images/dolphin.png'); ?>" />
Where and how do I include .js files in Views in CodeIgniter?
I have tried this:
<script type="text/javascript" src="system/application/libraries/jquery.js"></script>
As I figured that the index.php is the one that is loading the views, so I'm guessing that every time a page is loaded the current dir path is at root because index.php is located at root. Is this true?
The above line doesn't so I am doing something wrong. I have a CodeIgniter project. The path is like this:
localhost/CodeIgniter/system/application
so which path should I use to include my jquery.js file which is located in
localhost/CodeIgniter/system/application/libraries
when I want to load the jquery.js file in a View located here:
localhost/codeIgniter/system/application/views/till_view.php
There is a lesser-known solution that works really well with clean syntax and much more portability than hard-coding URL's or relative files.
<base href="<?=base_url();?>">
<img src="images/logo.gif" alt="Logo" />
Read more about how, what, why on my article "Asset handling in CodeIgniter with the BASE tag".
It's not the "index.php" file that is the view. The view is whatever is loaded in your controller when you do a
$this->load->view("viewname");
The file "viewname.php" can then include the .js files, just as a normal .html (or .php) file would:
<script src="/url/to/javascript.js" />
You may want to create a default view or a "header" view that includes some or all of the (common) .js files for your project.
-CF
First question:
It depends if you use absolute or relative urls.
Absolute urls go from the root of your domain. Relative urls are loaded relative from the current directory (including the url segments).
Second question:
It's best to use an absolute URL. Because of the pretty urls, it's not recommended to use relative urls.
The easiest way is to use the url helper and then use the site url function like this:
$this->load->helper('url');
echo site_url('system/application/libraries/jquery.js');
Ps. I recommend to put things like javascript and images outside of the CodeIgniter directory.
base_url() always works fine for me
Here is a solution specifically relevant to the OP which I didn't think anyone else provided.
<script type="text/javascript" src="<?= base_url() ?>libraries/jquery.js"></script>
So, base_url() will provide a path direct to the root of your site.
<script type="text/javascript" src="<?php base_url() ?>libraries/jquery.js"></script>
base_url() will provide the url of the site
The easiest way you can directly access the file:
<img src="http://localhost/ci/you_dir/your_img/your_image.png" />
where ci is for codeigniter.