Php codeigniter:从模型内加载变量和视图

Php codeigniter:从模型内加载变量和视图,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在Codeigniter中构建一个图像上传脚本(第一次),我的方式是,如果图像上传表单得到验证,它将在模型中执行以下代码: public function upload($id) //function to handle the initial image upload before crop { $config['image_library'] = 'gd2'; $config['source_image'] = '/path/to/image/mypic.jpg';

我正在Codeigniter中构建一个图像上传脚本(第一次),我的方式是,如果图像上传表单得到验证,它将在模型中执行以下代码:

public function upload($id) //function to handle the initial image upload before crop
{
    $config['image_library'] = 'gd2';
    $config['source_image'] = '/path/to/image/mypic.jpg';
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width']     = 75;
    $config['height']   = 50;

    $this->load->library('image_lib', $config); 

    $this->image_lib->resize();

    $config['upload_path'] = 'images/uploads/temp/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '2048';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);

    $file_data = $this->upload->data();

    if ( ! $this->upload->do_upload() )//the default parameter of do_upload() is 'userfile' (meaning the name of the form input) 
    {
        $error = '<p>Upload failed!</p> <p>Please make sure the image is in a .jpg, .gif or .png format and that it\'s no larger than 1028 x 768 pixels in dimension. Also it can\'t be more than 2MBs in size. If this problem persists, please contact <a href="mailto:webmaster@dreamsy.org">webmaster@dreamsy.org</a>.</p>';

        $this->session->set_flashdata('error', $error);
        redirect(base_url().'community/upload_image');
    }
    else
    {
        $image_path = base_url().'images/uploads/temp/'.$file_name;
        //$data = array( 'upload_data' => $this->upload->data() );

        //$this->load->view('community/upload_success');
        $this->load->helper('form', 'url');
        $vars['id'] = $this->uri->segment(3);
        $vars['$image_path'] = $image_path;
        $vars['$file_data'] = $file_data;
        $this->load->vars($vars); 
        $this->template->write_view('content', 'community/upload_success');
        $this->template->render();
    }
}>template->render(); //template library
public function upload($id)//在裁剪之前处理初始图像上载的函数
{
$config['image_library']='gd2';
$config['source_image']='/path/to/image/mypic.jpg';
$config['create_thumb']=TRUE;
$config['maintain_ratio']=TRUE;
$config['width']=75;
$config['height']=50;
$this->load->library('image_lib',$config);
$this->image_lib->resize();
$config['upload_path']='images/uploads/temp/';
$config['allowed_types']='gif | jpg | png';
$config['max_size']='2048';
$config['max_width']='1024';
$config['max_height']='768';
$this->load->library('upload',$config);
$file_data=$this->upload->data();
if(!$this->upload->do_upload())//do_upload()的默认参数是“userfile”(表示表单输入的名称)
{
$error='上载失败!

请确保图像为.jpg、.gif或.png格式,并且其尺寸不大于1028 x 768像素。此外,其大小不能超过2MB。如果此问题仍然存在,请与联系。

'; $this->session->set_flashdata('error',$error); 重定向(基本url().“社区/上传图片”); } 其他的 { $image\u path=base\u url().'images/uploads/temp/'.$file\u name; //$data=array('upload_data'=>$this->upload->data()); //$this->load->view('community/upload_success'); $this->load->helper('form','url'); $vars['id']=$this->uri->segment(3); $vars['$image\u path']=$image\u path; $vars['$file\u data']=$file\u data; $this->load->vars($vars); $this->template->write_view('content','community/upload_success'); $this->template->render(); } }>模板->渲染()//模板库
但是当我调用加载的变量(通过load->vars())时,它们不会被加载,当页面加载时,我会得到“undefined variable”错误。我怀疑即使从模型中加载视图,也不可能将变量从模型传递到视图,正如我前面所做的那样。或者我只是做错了什么(因为我有点像n00b)

你会走这条路吗?将变量传递给控制器,然后从控制器加载视图是否更有意义?或者其他我根本没有考虑过的事情?哈哈

非常感谢您的帮助。提前谢谢

*编辑: 我还在 你会走这条路吗?将变量传递给控制器,然后从控制器加载视图是否更有意义?或者其他我根本没有考虑过的事情

是的,一般来说,让视图直接与模型对话是可以接受的,反之亦然,但只有在使用表示层时。对于CI,我认为最好在渲染视图时将所有数据从控制器加载到视图中,如下所示:

$this->load->view('your-view', $data);
// Your_Model is the class name of your model
// your_model is the variable name it gets injected into
$this->load->model('Your_Model','your_model');

$data['some_data'] = $this->your_model->get_some_data();
function get_some_data()
{
    return $this->some_data;
}
要将变量从模型传递到控制器,可以对模型调用如下方法:

$this->load->view('your-view', $data);
// Your_Model is the class name of your model
// your_model is the variable name it gets injected into
$this->load->model('Your_Model','your_model');

$data['some_data'] = $this->your_model->get_some_data();
function get_some_data()
{
    return $this->some_data;
}
要在模型中定义这些getter,可以定义如下函数:

$this->load->view('your-view', $data);
// Your_Model is the class name of your model
// your_model is the variable name it gets injected into
$this->load->model('Your_Model','your_model');

$data['some_data'] = $this->your_model->get_some_data();
function get_some_data()
{
    return $this->some_data;
}
这样,您就可以在数据中创建一个API,让getter以您需要的格式检索您需要的任何内容

你会走这条路吗?将变量传递给控制器,然后从控制器加载视图是否更有意义?或者其他我根本没有考虑过的事情

是的,一般来说,让视图直接与模型对话是可以接受的,反之亦然,但只有在使用表示层时。对于CI,我认为最好在渲染视图时将所有数据从控制器加载到视图中,如下所示:

$this->load->view('your-view', $data);
// Your_Model is the class name of your model
// your_model is the variable name it gets injected into
$this->load->model('Your_Model','your_model');

$data['some_data'] = $this->your_model->get_some_data();
function get_some_data()
{
    return $this->some_data;
}
要将变量从模型传递到控制器,可以对模型调用如下方法:

$this->load->view('your-view', $data);
// Your_Model is the class name of your model
// your_model is the variable name it gets injected into
$this->load->model('Your_Model','your_model');

$data['some_data'] = $this->your_model->get_some_data();
function get_some_data()
{
    return $this->some_data;
}
要在模型中定义这些getter,可以定义如下函数:

$this->load->view('your-view', $data);
// Your_Model is the class name of your model
// your_model is the variable name it gets injected into
$this->load->model('Your_Model','your_model');

$data['some_data'] = $this->your_model->get_some_data();
function get_some_data()
{
    return $this->some_data;
}
这样,您就可以在数据中创建一个API,让getter以您需要的格式检索您需要的任何内容。

假设行: $this->load->vars($vars); 是要加载视图的位置,则应为: $this->load->view('nameofview',$vars)

假设行: $this->load->vars($vars); 是要加载视图的位置,则应为:
$this->load->view('nameofview',$vars)

如何将变量从模型传递到控制器?在这里读取视图数据:因此,我加载当前的模型函数,但不是在那里加载视图,而是让它返回一个数组,其中包含我要在视图中调用的数据,然后使用控制器精确地加载视图和数组。通过这种方式,模型只是数据,视图只是视图。我可以在视图中调用一个变量数组,从模型到控制器,并说:“return$vars”吗?因为这就是我尝试过的,它告诉我变量是未定义的,即使我有一个load->vars($vars)我的控制器中的行。您如何将变量从模型传递到控制器?在此处读取视图\ u数据:因此,我加载当前的模型函数,但不是在那里加载视图,而是让它返回一个数组,其中包含我要在视图中调用的数据,然后使用控制器加载视图和阵列?正是。通过这种方式,模型只是数据,视图只是视图。我可以在视图中调用一个变量数组,从模型到控制器,并说:“return$vars”吗?因为这就是我所尝试的,它告诉我变量是未定义的,即使我的控制器中有一个load->vars($vars)行