重构控制器到代码点火器的模型 [英] Refactoring Controller to Model in Code Igniter
问题描述
我注意到,我目前在我的控制器中的图像处理代码将更适合在一个模型,但我不知道哪里开始做这个。
我有一个控制器处理上传图像,重命名文件并使用Doctrine将其存储在数据库中:
<?php
class Addimage extends Controller
{
function index()
{
vars ['content_view'] ='uploadimage';
$ this-> load-> view('template',$ vars);
}
public function do_upload()
{
$ this-> load-> library('form_validation');
if($ this-> _submit_validate()== FALSE)
{
/ *这个代码块是从我的主页控制器复制的 - 这是我想重构的原因之一。* /
$ vars ['recentimages'] = Doctrine_Query :: create()
- > select('photo_path')
- > from('Gif g')
- > orderBy('g.created_at DESC')
- > limit(12)
- > execute()
$ vars ['title'] ='Home';
$ vars ['content_view'] ='welcome_message';
$ this-> load-> view('template_front',$ vars);
}
else
{
$ basedir = $ this-> config-> item('server_root')。 $ this-> config-> item('upload_dir');
//如果目录不存在,请创建它。
if(!is_dir($ basedir))
{
mkdir($ basedir,0777);
}
$ config = array(
'allowed_types'=>gif,
'upload_path'=> $ basedir,
'remove_spaces'=> true
);
$ this-> load-> library('upload',$ config);
if(!$ this-> upload-> do_upload())
{
$ data ['error'] ='上传有问题;
}
else
{
$ image_data = $ this-> upload-> data();
$ fileName = $ image_data ['file_name'];
$ title = $ this-> input-> post('title');
//根据多少字母重命名文件
//已经在数据库中
$ imageCount = Doctrine_Query :: create()
- > select('COUNT(i.id)as num_images')
- > from('Gif i')
- > execute()
$ imageCount = $ imageCount [0] - > num_images ++;
//根据db中的标题和图像数重命名文件。
$ newFileName = preg_replace('/ [^ a-zA-Z0-9\s] /','',$ title)。 '_'。 $ imageCount。 $ image_data ['file_ext'];
rename($ basedir。$ fileName,$ basedir。$ newFileName);
$ gif = new Gif();
$ gif-> photo_path = $ newFileName;
$ gif-> title = $ title;
if(Current_User :: user())
{
$ gif-> User = Current_User :: user
}
else
{
$ gif-> User = Doctrine :: getTable('User') - > findOneById($ this-> config-> item ('anonuid'));
}
$ gif-> save();
}
redirect('/','location');
}
}
私人函数_submit_validate()
{
$ this-> form_validation-> set_rules('title','Title' ,'required');
return $ this-> form_validation-> run();
}
}
在模型中的大部分,因为我使用模板系统的视图,我的uploadimage.php视图只是上传表单,以便它可以放在任何页面。感谢您提供任何帮助
我在我自己的项目有一个非常类似的问题:复制在控制器。我认为在你的情况下,将逻辑的一部分移动到模型是有意义的,因为大多数实际上是有意义的在一个控制器。
渲染视图绝对应该在控制器中,并且输入验证。我会将事务部分移动到模型:SQL,文件处理和图像处理。
然后你仍然会有一些重复,但我看不到其他方式,因为控制器逻辑和模型逻辑在这种情况下是如此交织。
It's come to my attention that my image processing code that I currently have in my controller would be better suited in a model, but I'm not sure even where to start to do this.
I have a controller that handles uploading an image, renaming the file and storing it in the database using Doctrine:
<?php
class Addimage extends Controller
{
function index()
{
$vars['content_view'] = 'uploadimage';
$this->load->view('template', $vars);
}
public function do_upload()
{
$this->load->library('form_validation');
if($this->_submit_validate() == FALSE)
{
/*THIS CODE BLOCK IS DUPLICATED FROM MY HOME PAGE CONTROLLER - this is one of the reasons I want to refactor.*/
$vars['recentimages'] = Doctrine_Query::create()
->select('photo_path')
->from('Gif g')
->orderBy('g.created_at DESC')
->limit(12)
->execute();
$vars['title'] = 'Home';
$vars['content_view'] = 'welcome_message';
$this->load->view('template_front', $vars);
}
else
{
$basedir = $this->config->item('server_root') . $this->config->item('upload_dir');
//If the directory doesn't already exist, create it.
if (!is_dir($basedir))
{
mkdir($basedir, 0777);
}
$config = array(
'allowed_types' => "gif",
'upload_path' => $basedir,
'remove_spaces' => true
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
$data['error'] = 'There was a problem with the upload';
}
else
{
$image_data = $this->upload->data();
$fileName = $image_data['file_name'];
$title = $this->input->post('title');
//Rename File based on how many of that letter
//are already in the database
$imageCount = Doctrine_Query::create()
->select('COUNT(i.id) as num_images')
->from('Gif i')
->execute();
$imageCount = $imageCount[0]->num_images++;
//Rename file based on title and number of images in db.
$newFileName = preg_replace('/[^a-zA-Z0-9\s]/', '', $title) . '_' . $imageCount . $image_data['file_ext'];
rename($basedir . $fileName, $basedir . $newFileName);
$gif = new Gif();
$gif->photo_path = $newFileName;
$gif->title = $title;
if(Current_User::user())
{
$gif->User = Current_User::user();
}
else
{
$gif->User = Doctrine::getTable('User')->findOneById($this->config->item('anonuid'));
}
$gif->save();
}
redirect('/', 'location');
}
}
private function _submit_validate()
{
$this->form_validation->set_rules('title', 'Title', 'required');
return $this->form_validation->run();
}
}
I would like to be able to have most of this in a model, because I'm using a template system for the views where my uploadimage.php view is just the upload form so that it can be dropped on any page. Also, I only have experience using Doctrine models.
Thanks for any help in advance
I had a very similar issue on my own project: duplication in the controllers. I think in your case it makes sense to only move parts of that logic into the model, because most of it actually makes sense to be in a controller.
Rendering view definitely should be in a controller, and input validation as well. I would move the transactional part to the model: the SQL, file handling and image manipulation.
You will then still have some duplication but I see no other way since controller logic and model logic are so interwoven in this case.
这篇关于重构控制器到代码点火器的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!