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 files after outputting the view. But apparently it is done before rendering the view. My question is, does $this->load->view();
have to be the final step in a function?
I did a little research, and seems like my question is similar to this topic. Correct?
Send the output to the browser before you run the command:
Otherwise you are just giving data to the Output class that will run as soon as the Controller is finished.
No. Actually for some web services I have just used something like this:
You can also have controller functions which are fully private from the web. Just prefix them with '_'. You would never view these as pages.
Perhaps you are misunderstanding MVC. The controller is an intermediary between the model and view. Likewise you don't need to use models either. The controller just getting stuff from models and using the view to print it out. There is no reason (although it would be bad style) you cannot use data from within the controller or from other sources or print out your page from the controller.
I don't see why it won't work if you load the view first. Although I would do it the other way around so you can offer feedback to the user. You should display an error to them if the files fail to move. If it is performance intensive consider using a queue or cron jobs.
Why don't you just use a
post_system
hook? It's called after the final page is sent to the browser, that way you can load views normally, without echoing them out.Here's an example controller:
And an example hook:
Check out the hooks user guide for info on setting them up. They are your friends!
EDIT: Something I just thought of...
If you're only going to be doing this inside one controller method... it would probably be better to use Phil's approach. This would avoid having a hook call for every request which would be unnecessary if you only need it once.
Another thing you could do, if you only need it once, is use CI's
_output()
handler for Controllers (info here). That would work like this: