是否codeIgniter在最后一步加载看法? [英] Does CodeIgniter have to load view in the final step?
问题描述
我有一个函数
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?
推荐答案
你为什么不只是使用 post_system
挂钩?最终的页面发送到浏览器后,这就是所谓的,这样你可以正常加载的意见,而不回显出来。
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:
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");
}
}
和一个示例钩子:
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}");
}
}
查看挂钩用户指南信息上设置起来。他们是你的朋友!
Check out the hooks user guide for info on setting them up. They are your friends!
编辑:东西我只是想的...
如果你只打算做这里面一个控制器方法......它可能会更好地使用菲尔的做法。这将避免一个钩呼吁这将是不必要的,如果你只需要一次请求。
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.
你可以做,如果你只需要一次,是用CI的 _output()
处理程序控制器(的此信息)。将工作是这样的:
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:
class Home extends Controller {
// Initalize the var to avoid having to
// check if it's 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}");
}
}
}
这篇关于是否codeIgniter在最后一步加载看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!