Should CodeIgniter load the view in the last step?

I have a function

function do_something() { // process $this->load->view('some_view', $data); exec('mv /path/to/folder1/*.mp3 /path/to/folder2/'); } 

My intention is to move the files after the output of the view. But, apparently, this is done before the visualization of the view. My question is whether $this->load->view(); be the last step in a function?

I did a little research and it looks like my question is similar to this section . Correctly?

+1
source share
4 answers

Why don't you just use the post_system hook? It is called after the last page has been sent to the browser, so you can load views normally without repeating them.

Here's an example of a controller:

 class Home extends Controller { function index() { $this->move_audio = TRUE; $this->old_folder = "/path/to/folder/"; $this->new_folder = "/path/to/folder2/"; $this->load->view("some_view"); } } 

And an example hook:

 function post_system() { $CI =& get_instance(); if( isset($CI->move_audio) && $CI->move_audio === TRUE) { // Trim, then add trailing slash for consitency $old_folder = rtrim($CI->old_folder, "/")."/*.mp3"; $new_folder = rtrim($CI->new_folder, "/")."/"; exec("mv {$old_folder} {$new_folder}"); } } 

Pay attention to the drag and drop user manual for information on setting them up. They are your friends!

EDIT: something I was just thinking about ...

If you intend to do this in only one controller method, it is probably best to use the Phil approach. This would avoid calling the hook for each request, which would be superfluous if you need it only once.

Another thing you could do if you only need to be done once is to use the CI _output() handler for the controllers ( information here ). This will work as follows:

 class Home extends Controller { // Initalize the var to avoid having to // check if it set or not var $move_audio = FALSE; // CONTROLLER METHOD function index() { $this->move_audio = TRUE; $this->old_folder = "/path/to/folder/"; $this->new_folder = "/path/to/folder2/"; $this->load->view("some_view"); } // OUTPUT HANDLER function _output($output = "") { echo $output; if($this->move_audio === TRUE) { // Trim, then add trailing slash for consitency $old_folder = rtrim($this->old_folder, "/")."/*.mp3"; $new_folder = rtrim($this->new_folder, "/")."/"; exec("mv {$old_folder} {$new_folder}"); } } } 
+2
source

Send the output to the browser before running the command:

 function do_something() { // process echo $this->load->view('some_view', $data, TRUE); exec('mv /path/to/folder1/*.mp3 /path/to/folder2/'); } 

Otherwise, you simply pass the data to the Output class, which will start as soon as the controller completes.

+1
source

Not. In fact, for some web services, I just used something like this:

 function get_json_search() { $results = $this->My_model->get_stuffs(); echo json_encode($results); } 

You can also have controller functions that are completely closed from the Internet. Just attach them to "_". You will never see them as pages.

 function _upload_photo($filepath) { //resize it and so on 

You may be misunderstanding MVC. The controller mediates between the model and the view. Also you do not need to use models. The controller simply receives material from the models and uses the view to print it. There is no reason (although it would be a bad style), you cannot use data from the controller or from other sources or print your page from the controller.

I do not understand why this will not work if you download the view first. Although I would do it the other way around so that you can offer user feedback. You must show them an error if the files cannot be moved. If this is workload, consider using queue or cron jobs.

0
source
 <?php while (ob_get_level()) { ob_end_flush(); } // start output buffering if (ob_get_length() === false) { ob_start(); } class Something extends Controller { function do_something() { $this->load->view('some_view', $data); ob_flush(); flush(); exec('mv /path/to/folder1/*.mp3 /path/to/folder2/'); } } ?> 
0
source

All Articles