Zend Framework 和防止肥胖控制器

发布于 2024-09-12 00:32:08 字数 2598 浏览 6 评论 0原文

避免胖控制器

所以我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光磨忆 2024-09-19 00:32:08

服务层可能是我要走的路。因此,您将创建一个如下所示的服务类:

class Your_Service_Import
{
    public function importFromCsv($csv)
    {
        // etc.
    }
}

然后将 csv_to_array 调用之后的所有控制器方法代码移至该方法中,使控制器方法的末尾看起来像这样:

$feed = $this->csv_to_array(trim($response->getBody()));

$service = new Your_Service_Import();
$service->importFromCsv($feed);

这使得它更容易测试您的导入代码(因为它位于独立的类中)并且更容易在应用程序的其他部分中重用。

Service layer is probably the way I'd go. So you'd create a service class that looks something like this:

class Your_Service_Import
{
    public function importFromCsv($csv)
    {
        // etc.
    }
}

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:

$feed = $this->csv_to_array(trim($response->getBody()));

$service = new Your_Service_Import();
$service->importFromCsv($feed);

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.

眼眸里的快感 2024-09-19 00:32:08

我会比@Tim Fountain 更进一步一步(或两步)

  1. 创建一个服务或域助手,它需要开始、结束(并且可以使用用户名密码和 url 进行配置)并将 csv 列表作为数组返回。
  2. 创建一个映射已知维度数组(csv)的服务并将其映射到数据库。

然后你的控制器将是

$start = $this->getRequest()->getParam('start');
$end = $this->getRequest()->getParam('end');
$dataService = new Your_Service_Get();
$data = $dataService->get($start, $end);
$mapService = new Your_Service_Map();
$mapService->map($data);

I'd go one step (or two steps) further than @Tim Fountain

  1. Create a Service or Domain Helper that takes a start, end (and can be configured with a username password and url) and returns the csv list as an array.
  2. Create a Service that maps a known dimension array (the csv) and maps it onto the database.

Your controller will then just be

$start = $this->getRequest()->getParam('start');
$end = $this->getRequest()->getParam('end');
$dataService = new Your_Service_Get();
$data = $dataService->get($start, $end);
$mapService = new Your_Service_Map();
$mapService->map($data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文