0

This is how the directory structure looks like in my codeigniter installation.

Root
-www
-lib
--application
--assets
---js

I am looking to get path of a controller in the javascript file located at js folder. How can I do that ?

Edit: I need the path to the php script for an Ajax call.

5
  • Using JS? or using PHP? When you say get, what do you mean? Commented Aug 11, 2015 at 11:43
  • i guess in order to obtain the right answer.. we would need to know why do you need this? do you need the full absolute path? by knowing this we might give you a better answer Commented Aug 11, 2015 at 12:06
  • @CodeGodie Yes, I think you are right. I have updated the original. Commented Aug 11, 2015 at 12:09
  • 1
    if it is for ajax, then you dont need the absolute file path. you need the URL which you can do with base_url("controller/method") like @Rejoanul suggested. Have you tried that? Commented Aug 11, 2015 at 12:11
  • if ajax then use site_url() instead of base_url(). does not need link main controller just use site_url('controller/method') it will work Commented Aug 11, 2015 at 14:18

1 Answer 1

1

you can get corresponding controller name by using $this->router->fetch_class()

make a hidden field in your html file

<input type="hidden" id="con_path" value="<?php echo base_url('application/controllers').'/'.$this->router->fetch_class();?>"/>

then grab it in your JS file

var con_path = $('#con_path').val();
Sign up to request clarification or add additional context in comments.

3 Comments

base_url will not give you the file path
so what about this? <input type="hidden" id="con_path" value="<?php echo base_url('application/controllers').'/'.ucfirst($this->router->fetch_class()).'.php';?>"/>
depends on why he wants to use it.. he probably needs the absolute file path rather than the url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.