4

I wanted to ask if there is any way i can get path of my view file in controller. For example

class welcome extends controller{

  function __construct(){
       parent::__construct();
  }
  function index(){
     $this->load->view('welcome_message');
  }
  function test(){
     $my_variable = $this->load->view('welcome_message','',TRUE);
  }
  function another_test(){
        ///  $path_to_view = ???;
        ///  echo $path_to_view;
  } 
}

i want to ask if there is any helper function to get this. The test method has variable containing the html contents in it. But i want to get path of view file???

2
  • What do you really want to do? Jun 21, 2012 at 9:24
  • i am trying to use this class which requires path of view file.This converts html into pdf. i know there are many other converter classes but i wes only curious to know how to get path of view file phpclasses.org/package/… Jun 21, 2012 at 9:27

2 Answers 2

9

I'm not sure if this is a proper way but you can try this, just create a helper file i.e. my_helper.php in your application/helper folder and paste following function in this helper file

function get_view_path($view_name)
{
    $target_file=APPPATH.'views/'.$view_name.'.php';
    if(file_exists($target_file)) return $target_file;
}

To use it you have to load the helper file first and then call the function with the name of the view as the function's argument

$this->load->helper('my_helper');
$path_to_view = get_view_path('welcome'); // Will return the path if welcome.php exists in the view folder.

You can auto load it using config.php's $autoload['helper'] = array('functions_helper');

2
  • Please let me know if you get something better than this.
    – The Alpha
    Jun 21, 2012 at 11:16
  • 1
    well i can create a function now in uri class then i will be able to use it like this $this->uri->get_path('my_view',$type); where $type = 'view' others can be model , controller. I like using class way of calling functions instead of straight function calls Jun 21, 2012 at 11:19
7

It should be the constant:

VIEWPATH
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.