How can I use relative path in CodeIgniter? - php

I know a little bit about relative path. Suppose, I have a project on codeigniter. There are 4 controller such as with function name:
1. Admin->index,profile,add_student,edit_marks,make_admin,
2. Teacher->index,add_marks,update_marks
3. Student->index,view_teacher,view_result
4. Guest->index,view_Student, view_marks,view_result
From these, How can I use them as relative path ?Remind it,some times I go one controller to another controller, such as profile,view_marks is a common function for all, so every one access it.
For images or file can I use relative path ? Then,Suppose I have a image at root folder as
->root_folder/template/images/a.png. How can I use it as relative path ?
Really, I need the core knowledge of relative path with application in codeigniter.

You should use codeigniter constants defined in root/index.php
To get Root path use
FCPATH
It returns
Path upto root folder where your codeigniter is installed.
So in your case , it will be
FCPATH . 'template/images/a.png';
I assume you're looking for path not URL.

At last I have found the answer that is:
It is not necessary to use base_url or site_url every times for link.
All time use:
/controller_name/function_name
Such as for a anchor link on viewing admins's profile code as(I hope, this technique is called relative path). It decrease time to route it.
Admin Profile

What's wrong with using this for the image path?
base_url('path/to/image')

If having senario like
ROOT
|____APPLICATION
|____ASSETS
| |________ CSS
| |________ JS
| |________ IMG
|____SYSTEM
then you can access the css as follows:-
<script type="text/javascript" src="<?php echo base_url();?>assets/css/style.css"></script>
<script type="text/javascript" src="<?php echo base_url();?>assets/js/getinfo.css"></script>
<script type="text/javascript" src="<?php echo base_url();?>assets/img/image.png"></script>

The current accepted answer works fine, but it is not relative. For instance: if the website would be replaced from the root to a subdirectory, the url wouldn't work anymore.
More on this same topic I found here: How can I display images with the Codeigniter php framework?
The answer in that topic recommends not working with relative paths, but personally I would like to see this possible within modules (the MX-plugin for CodeIgniter).
So here's a possible way of working with relative paths I found:
<?php $path = base_url(str_replace(FCPATH,"", __DIR__ )).'/'; ?>
Example usage:
<img src="<?php echo $path;?>../assets/img/myImage.png" />
for instance: if the this html would be used in /modules/mymodule/views/someviewfile.php the output will be yourdomain.com/modules/mymodule/assets/img/myImage.png

Related

does using different relative paths for the same resource get cached by the browser just once?

My question might not be clear, so here is an example.
I have a PHP script that will auto add the relative path to all of HTML resources like
CSS - <link href href="<?php echo $siteroot ?>css/main.css" ... >
JS - <script src="<?php echo $siteroot ?>js/main.js"</script>
Images <img src="<?php echo $siteroot ?>img/avatar.jpg" ... >
other uses like PHP includes
the script will auto make the relative path to the site root and this will vary to be '../', '../../', '../../../', or an empty string '' if it is the site root folder - main index
My question does this will affect the cache system the browser uses? I thought of this because the same resource will be different in many pages that are in subfolders!
once ../../img/avatar.jpg other ../img/avatar.jpg, other img/avatar.jpg alone!
I have tried with chrome and run a file with img and then deleting the img and opening a file in a subfolder. This worked and the image was there!
I still not sure and want a granted answer about the caching process for relative paths. If there is any extra information, I will appreciate it :)
Thank you
These resources will be cached once because absolute path is same, regardless they relative paths differ. Browsers uses absolute paths for caching.

CodeIgniter relative path or base URL function?

All these days I thought that CodeIgniter did not allow direct access to file(s) in the application (I mean the application itself and not the application folder).
So, if I have the following structure under the folder, www:
ROOT
|____APPLICATION
|___________JS/mine.js
|___________VIEWS/my_view.php
And if I want to include mine.js in my_view.php, I would need to refer the JS file using the base_url() function as follows:
<script type="text/javascript" src="<?php echo base_url();?>js/mine.js"></script>
Looks like I was wrong, I can refer to it relatively as well. Like:
<script type="text/javascript" src="../js/mine.js"></script>
Any thoughts/opinions?
Which one is a good practice? And why?
It's always a smart idea to use base_url() so if your URL changes, all your links don't break.
base_url() is going to return the full TLD, plus any folders your site is in.
Best practice is to keep your assets out of the application, or any other system folders. Most CodeIgniter sites will structure their assets as follows:
ROOT
|____APPLICATION
|____ASSETS
| |________ CSS
| |________ JS
| |________ IMG
|____SYSTEM
And then referenced like so:
<script type="text/javascript" src="<?php echo base_url('assets/js/mine.js')"></script>
It is always a good idea while using MVC to keep all assets folders (css, js and images) in root directory rather than keeping inside application or any other folder and accessing them using base_url.
<script type="text/javascript" src="<?php echo base_url();?>js/mine.js"></script>
So I think above line you mentioned is the proper way.

Which type of URL is better for Laravel?

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.

CodeIgniter: How do I include a .js file in view?

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.

PHP: Defining relative paths compatible with HTML Tags

Consider the following directory structure:
ROOT
------ images
............... logo.png
------ includes
............... vars.php
------ layout
............... content.php
------ index.php
How do I define a path constant for logo.png in vars.php that is accessible in both index.php and content.php? Should be compatible with HTML Tags as a relative path.
<img src="<?php echo IMAGE_PATH; ?>">
which should be parsed as
<img src="images/logo.png"> <!-- if used in index.php -->
and
<img src="../images/logo.png"> <!-- if used in content.php -->
New Question (EDIT): Does root-relative path work when including php files using include / require methods?
Try setting the <base> tag in the <head> section of your code.
All your images, css, and js files will use this instead of the url in the address bar.
Info on base
Absolute url or root paths will give you the least amount of headaces. Trust me, when the system grows you'll regret that setup.
It is a perfectly legal way to reference things. (as you ask in the comments)
If you're worried about setups between domains, just create a config variable with the absolute path to the domain / directory / etc
You can use "root-relative" paths. Simply link to everything with a forward slash at the beginning, i.e.
<img src="/images/logo.png">
This will resolve to http://yoursite.com/images/logo.png from every page on yoursite.com.
simply specify all paths as relative to the root
<img src="/images/logo.png"> <!-- will work anywhere -->
I'd suggest, primarily, that you use root-relative paths. This is only to reduce the complications of moving your site to another host, and also it allows for consistent paths (rather than using an if() condition to test from where the script's being run).
But otherwise, your suggestion would be fine.
I would use something like an application base URL:
define('APP_URL', 'http://example.com/path/to/app');
echo '<img src="'.APP_URL.IMAGE_PATH.'">';
Or to have it more convenient, write a function that resolves your relative URL to an absolute URL.

Categories