Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get path of view file in codeigniter

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???

like image 421
Muhammad Raheel Avatar asked Dec 04 '22 15:12

Muhammad Raheel


1 Answers

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');

like image 101
The Alpha Avatar answered Dec 11 '22 09:12

The Alpha