English 中文(简体)
泽德框架和防止法特主计长
原标题:Zend Framework and Preventing Fat Controllers

Avoiding Fat Controller

因此,我使用Zentd Framework,我有一个问题,涉及防止心脏控制器采取我的行动。 基本上,我正在将CSV档案整理成我的数据库。

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.

我正在使用Zentd Framework的数据绘图仪。 我是在我的主计长中这样做的。 所有这一切都令人难以置信,我感到,在错误的地方这样做了吗? 如果我通过整个<$-feed,然后在这个类别中,我即时宣读我的模型和我的地图,那就是一个更好的选择?

此外,我需要实现正常化,这意味着我应该利用交易,但我不敢在我开始交易的时候这样做。 由于我目前的工作方式,我可以考虑的唯一地点是我的主计长。 wow. 这将是一个可贵的地方。

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);

}
最佳回答

服务层面可能是我走的路。 因此,你创立了一个服务班,研究类似情况:

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

接着,你把所有控制器法文本移至 c号之后,以便采用这种方法。

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

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

这使得测试贵国的进口代码(因为它属于单独类别)更加容易,并且更容易在你申请的其他部分重复使用。

问题回答

I d 超出@Tim喷洒的一步(或两个步骤)。

  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.

届时,你的控制者将马上到来。

$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);




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签