Zend Framework 和防止肥胖控制器
避免胖控制器
所以我正在使用 Zend Framework,并且我有一个问题涉及通过我的操作之一来防止胖控制器。基本上我正在将 CSV 文件规范化到我的数据库中。
这意味着我必须获取提要然后使用我的模型。 抓取提要只是为了展示它是如何工作的,但现在它是一个动作助手。
我正在使用 Zend Framework 的数据映射器模式。我讨厌在我的控制器中这样做。所有这些 setProperty()->setProperty()->setProperty() 看起来都非常丑陋,我觉得我在错误的地方做错了?创建某种服务层,在其中传递整个$feed,然后在该类中实例化我的模型和映射器,这是一个更好的选择吗?
另外,我需要标准化,这意味着我应该使用事务,但我不确定应该从哪里开始事务。由于我目前做事的方式,我唯一可以考虑的地方就是我的控制器。 哇..那将是一个可怕的地方。
如何从控制器中获取模型行为和操作?
ImportController.php
public function indexAction() {
$start = $this->getRequest()->getParam('start');
$end = $this->getRequest()->getParam('end');
$url = "http://www.domain.com/admin/GetBookingData.aspx";
$client = new Zend_Http_Client();
$client->setParameterGet('dateEnteredMin', $start);
$client->setParameterGet('dateEnteredMax', $end);
$client->setParameterGet('login', 'login');
$client->setParameterGet('password', 'password');
$client->setUri( $url );
$client->setConfig(array(
'maxredirects' => 0,
'timeout' => 30));
// Send the request.
$response = $client->request();
// Grab the feed from ->getBody and add it to $feed
$feed = $this->csv_to_array(trim($response->getBody()));
// The first item in the array is the heading in the CSV, so we can remove it from the array using shift().
$title = array_shift($feed);
// Create my Models and Mappers.
// *** EVERYTHING BELOW HERE IS WHAT I DON'T LIKE ***
$bookings = new Bookings_Models_Bookings();
$property = new Bookings_Models_Property();
$clients = new Bookings_Models_Clients();
$bookingsMapper = new Bookings_Models_Bookings_Mapper();
$propertyMapper = new Bookings_Models_Property_Mapper();
$clientsMapper = new Bookings_Models_Clients_Mapper();
$bookings->setId($feed[9])
->setPropertyId($feed[1])
->setClientId($feed[2])
->setDate($feed[4]);
$bookingsMapper->save($bookings);
$property->setId($feed[1])
->setPropertyName($feed[23])
$propertyMapper->save($bookings);
$clients->setId($feed[2])
->setFirstName($feed[20])
->setLastName($feed[21])
$clientsMapper->save($clients);
}
Avoiding Fat Controller
So I'm using Zend Framework and I have a question involving preventing fat controllers with one of my actions. Basically I am normalizing a CSV file into my database.
This means that I have to get the feed and then use my model.
The feed grabbing is just there to show how it works, but that is now an Action Helper.
I am using the Data Mapper pattern with Zend Framework. I hate that I am doing this in my Controller. All of those setProperty()->setProperty()->setProperty() look incredibly fugly and I feel like I am doing it in the wrong place? Would it be a better option to just create some kind of service layer where I pass the entire $feed and then in that class I instantiate my Models and my Mapper?
Also, I need to normalize, which means I should be using a transaction, but I'm unsure where I should start my transaction. Because of the way I am doing things currently, the only place I could ever consider is in my Controller. wow.. that would be an awful place.
How can I get the model behaviour and operations out of my controller?
ImportController.php
public function indexAction() {
$start = $this->getRequest()->getParam('start');
$end = $this->getRequest()->getParam('end');
$url = "http://www.domain.com/admin/GetBookingData.aspx";
$client = new Zend_Http_Client();
$client->setParameterGet('dateEnteredMin', $start);
$client->setParameterGet('dateEnteredMax', $end);
$client->setParameterGet('login', 'login');
$client->setParameterGet('password', 'password');
$client->setUri( $url );
$client->setConfig(array(
'maxredirects' => 0,
'timeout' => 30));
// Send the request.
$response = $client->request();
// Grab the feed from ->getBody and add it to $feed
$feed = $this->csv_to_array(trim($response->getBody()));
// The first item in the array is the heading in the CSV, so we can remove it from the array using shift().
$title = array_shift($feed);
// Create my Models and Mappers.
// *** EVERYTHING BELOW HERE IS WHAT I DON'T LIKE ***
$bookings = new Bookings_Models_Bookings();
$property = new Bookings_Models_Property();
$clients = new Bookings_Models_Clients();
$bookingsMapper = new Bookings_Models_Bookings_Mapper();
$propertyMapper = new Bookings_Models_Property_Mapper();
$clientsMapper = new Bookings_Models_Clients_Mapper();
$bookings->setId($feed[9])
->setPropertyId($feed[1])
->setClientId($feed[2])
->setDate($feed[4]);
$bookingsMapper->save($bookings);
$property->setId($feed[1])
->setPropertyName($feed[23])
$propertyMapper->save($bookings);
$clients->setId($feed[2])
->setFirstName($feed[20])
->setLastName($feed[21])
$clientsMapper->save($clients);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
服务层可能是我要走的路。因此,您将创建一个如下所示的服务类:
然后将 csv_to_array 调用之后的所有控制器方法代码移至该方法中,使控制器方法的末尾看起来像这样:
这使得它更容易测试您的导入代码(因为它位于独立的类中)并且更容易在应用程序的其他部分中重用。
Service layer is probably the way I'd go. So you'd create a service class that looks something like this:
you'd then move all of your controller method code that's after the csv_to_array call into that method, leaving the end of your controller method looking something like this:
This makes it easier to test your import code (since it's in a standalone class) and easier to reuse in other parts of your application.
我会比@Tim Fountain 更进一步一步(或两步)
然后你的控制器将是
I'd go one step (or two steps) further than @Tim Fountain
Your controller will then just be