7.8. MVC (À¸)·ÎÀÇ ¿¹¿Ü

7.8.1. µµÀÔ

Zend Framework ÀÇ MVC ÄÄÆÛ³ÍÆ®´Â, ÇÁ·±Æ® ÄÜÆ®·Ñ·¯¸¦ »ç¿ëÇϰí ÀÖ½À´Ï´Ù. Áï, ¾î´À »çÀÌÆ®¿¡ ´ëÇÑ ¸ðµç ¸®Äù½ºÆ®¸¦ ÇϳªÀÇ ¿£Æ®¸® Æ÷ÀÎÆ®·Î ó¸®ÇÏ´Â °ÍÀÔ´Ï´Ù.±× °á°ú, ¸ðµç ¿¹¿Ü´Â ÃÖÁ¾ÀûÀ¸·Î ÇÁ·±Æ® ÄÜÆ®·Ñ·¯¿¡ µµ´ÞÇÏ°Ô µË´Ï´Ù. °³¹ßÀÚ´Â, ¿¹¿Ü¸¦ ¿©±â¼­ Á¤¸®ÇØ Ã³¸®ÇÒ ¼ö ÀÖ½À´Ï´Ù.

±×·¯³ª, ¿¹¿ÜÀÇ ¸Þ¼¼Áö³ª ¹é Æ®·¹À̽º¾È¿¡´Â, ½Ã½ºÅÛÀÇ Áß¿äÇÑ Á¤º¸°¡ Æ÷ÇԵǾî ÀÖ´Â ÀÏÀÌ ÀÖ½À´Ï´Ù. ¿¹¸¦ µé¾î SQL ¹®ÀåÀÇ ³»¿ëÀ̳ª ÆÄÀÏÀÇ À§Ä¡¶ó°í ÇÏ´Â Á¤º¸ÀÔ´Ï´Ù. ´ç½ÅÀÇ »çÀÌÆ®¸¦ Áö۱â À§ÇØ, µðÆúÆ®¿¡¼­´Â Zend_Controller_Front ÇÏÁö¸¸ ¸ðµç ¿¹¿Ü¸¦ Æ÷ÂøÇØ, ±×°ÍÀ» ¸®½ºÆù½º ¿ÀºêÁ§Æ®¿¡ µî·ÏÇÏ°Ô µÇ¾î ÀÖ½À´Ï´Ù. ¶Ç, ¸®½ºÆù½º ¿ÀºêÁ§Æ®´Â, µðÆúÆ®¿¡¼­´Â ±×·¯ÇÑ ¿¹¿Ü ¸Þ¼¼Áö¸¦ Ç¥½ÃÇÏÁö ¾Ê½À´Ï´Ù.

7.8.2. ¿¹¿Ü¸¦ ó¸®ÇÏ´Â ¹æ¹ýÀº?

MVC ÄÄÆÛ³ÍÆ®³»¿¡¼­ ¿¹¿Ü¸¦ ó¸®Çϱâ À§ÇÑ ±¸Á¶°¡ Â¥³Ö¾îÁö°í ÀÖ½À´Ï´Ù.

  • Zend_Controller_Front::throwExceptions()

    ÀÌ ¸Þ¼Òµå¿¡ true (À»)¸¦ °Ç³×ÁÖ¸é, ÇÁ·±Æ® ÄÜÆ®·Ñ·¯°¡ ¸®½ºÆù½º ¿ÀºêÁ§Æ®¿¡ ¿¹¿Ü¸¦ Á¤¸®ÇÏ´Â ´ë½Å¿¡ ¿¹¿Ü¸¦ ÀÚ±â ÀÚ½ÅÀ¸·Î ó¸®ÇÒ ¼ö ÀÖ°Ô µË´Ï´Ù.¿¹¸¦ µé¾î ´ÙÀ½°ú °°ÀÌ µË´Ï´Ù.

    
    <?php
    $front
    ->throwExceptions(true);
    try {
        
    $front->dispatch();
    } catch (
    Exception $e) {
        
    //  ¿©±â¼­, ÀÚ±â ÀÚ½ÅÀ¸·Î ¿¹¿Ü¸¦ ó¸®ÇÕ´Ï´Ù
    }

    À̰ÍÀÌ, ÀÚ½ÅÀÇ ¾îÇø®ÄÉÀ̼ÇÀ¸·Î µ¶ÀÚÀûÀÎ ¿¹¿Ü 󸮸¦ ÇàÇϱâ À§ÇÑ ¹«¾ùº¸´Ù °£´ÜÇÑ ¹æ¹ýÀÌÁö¿ä.

  • Zend_Controller_Response_Abstract::renderExceptions()

    ÀÌ ¸Þ¼Òµå¿¡ true (À»)¸¦ °Ç³×ÁÖ¸é, ¸®½ºÆù½º ¿ÀºêÁ§Æ®¸¦ ·»´õ¸µ ÇÒ ¶§¿¡ ¿¹¿Ü ¸Þ¼¼Áö³ª ¹é Æ®·¹À̽ºµµ Ç¥½ÃÇÏ°Ô µË´Ï´Ù. À̰ÍÀ» ½Ç½ÃÇϸé, ¾îÇø®ÄÉÀ̼ÇÀ¸·Î ¹ß»ýÇÑ ¸ðµç ¿¹¿Ü°¡ Ç¥½ÃµÇ°Ô µË´Ï´Ù. ½ÇÁ¦ÀÇ ¿î¿ë ȯ°æ À̿ܿ¡¼­¸¸ »ç¿ëÇϵµ·Ï ÇսôÙ.

  • Zend_Controller_Front::returnResponse() ¹× Zend_Controller_Response_Abstract::isException()

    Zend_Controller_Front::returnResponse() ¿¡ true (À»)¸¦ °Ç³×ÁÖ¸é, Zend_Controller_Front::dispatch() (Àº)´Â ¸®½ºÆù½º¸¦ ·»´õ¸µ ÇÏÁö ¾Ê°í, ±×´ë·Î µ¹·ÁÁÝ´Ï´Ù.¸®½ºÆù½º¸¦ ¹ÞÀº ´ÙÀ½¿¡, ó¸®ÇØ¾ß ÇÒ ¿¹¿Ü°¡ ÀÖÀ»Áö¸¦ isException() ¸Þ¼Òµå·Î Á¶»çÇØ ±× ³»¿ëÀ» getException() ¸Þ¼Òµå·Î ÃëµæÇÕ´Ï´Ù. ¿¹¸¦ µé¾î ´ÙÀ½°ú °°ÀÌ µË´Ï´Ù.

    
    <?php
    $front
    ->returnResponse(true);
    $response $front->dispatch();
    if (
    $response->isException()) {
        
    $exceptions $response->getException();
        
    //  ¿¹¿Ü¸¦ ó¸®ÇÕ´Ï´Ù ...
    } else {
        
    $response->sendHeaders();
        
    $response->outputBody();
    }

    Zend_Controller_Front::throwExceptions() ¿¡ ºñÇØ ÀÌ ¹æ¹ýÀÌ ¿ì¼öÇÑ Á¡Àº, ¿¹¿Ü¸¦ ó¸®ÇÑ ´ÙÀ½¿¡, ±×°ÍÀ» ·»´õ¸µ ÇÒÁö¸¦ ÆÇ´ÜÇÒ ¼ö ÀÖ´Â °÷(Áß)ÀÔ´Ï´Ù.

7.8.3. MVC ±×¸®°í Á¶¿ìÇÒ ¿¹¿Ü

°¢ MVC ÄÄÆÛ³ÍÆ®±º -- ¸®Äù½ºÆ®, ¶ó¿ìÅÍ, µð½ºÆÖÃÄ, ¾×¼Ç ÄÜÆ®·Ñ·¯ ±×¸®°í ¸®½ºÆù½º ¿ÀºêÁ§Æ® -- (Àº)´Â °¢°¢ ¿¹¿Ü¸¦ ½½·Î¿ì ÇÕ´Ï´Ù. Á¶°Ç¿¡ µû¶ó¼­´Â µ¡¾²±âµÇ´Â ¿¹¿Üµµ ÀÖ°í, ¾È¿¡´Â °³¹ßÀÚ°¡ ¾îÇø®ÄÉÀ̼ÇÀÇ ±¸Á¶¸¦ Àç°ËÅäÇÏÁö ¾ÊÀ¸¸é ¾È µÇ´Â µíÇÑ ¿¹¿Üµµ ÀÖ°ÚÁö¿ä.

¸î°³Àΰ¡¿¹¸¦ ³ªÅ¸³À´Ï´Ù.

  • Zend_Controller_Dispatcher::dispatch() (Àº)´Â, µðÆúÆ®¿¡¼­´Â, ¹«È¿ÀÎ ÄÜÆ®·Ñ·¯°¡ ¸®Äù½ºÆ® µÇ¾úÀ» ¶§¿¡ ¿¹¿Ü¸¦ ½½·Î¿ì ÇÕ´Ï´Ù. ÀÌ ¿¹¿Ü¿¡ÀÇ ´ëó ¹æ¹ýÀ¸·Î¼­´Â, ´ÙÀ½ÀÇ µÑÀ» Ãßõ ÇÕ´Ï´Ù.

    • ÆÄ¶ó¹ÌÅÍ 'useDefaultControllerAlways' (À»)¸¦ ¼³Á¤ÇÕ´Ï´Ù.

      ÇÁ·±Æ® ÄÜÆ®·Ñ·¯³ª µð½ºÆÖÃÄÀÇ ¾î´ÀÂÊÀ̵çÀ¸·Î, ÀÌÇÏÀÇ Áö½Ã¹®À» Ãß°¡ÇÕ´Ï´Ù.

      
      <?php
      $front
      ->setParam('useDefaultControllerAlways'true);

      //  ȤÀº
      $dispatcher->setParam('useDefaultControllerAlways'true);

      ÀÌ Ç÷¡±×¸¦ ¼³Á¤Çϸé, µð½ºÆÖÃÄ´Â ¿¹¿Ü¸¦ ½½·Î¿ì ÇÏÁö ¾Ê°í, ´ë½Å¿¡ µðÆúÆ®ÀÇ ÄÜÆ®·Ñ·¯¿Í ¾×¼ÇÀ» »ç¿ëÇÏ°Ô µË´Ï´Ù. ÀÌ ¹æ½ÄÀÇ °áÁ¡Àº, À¯Àú°¡ »çÀÌÆ®ÀÇ ÁÖ¼Ò¸¦ À߸ø ÃÆÀ» ¶§¿¡ µµ Ȩ ÆäÀÌÁö°¡ Ç¥½ÃµÇ¾î ¹ö¸®´Â °ÍÀÔ´Ï´Ù.À̰ÍÀº ½áÄ¡¿£Áø ÃÖÀûÈ­¸¦ ¾û¸ÁÀ¸·Î ÇØ ¹ö¸± °¡´É¼ºÀÌ ÀÖ½À´Ï´Ù.

    • dispatch() ÇÏÁö¸¸ ½½·Î¿ì ÇÏ´Â ¿¹¿Ü´Â Zend_Controller_Dispatcher_Exception ±×¸®°í, ÀÌ ¾È¿¡´Â 'Invalid controller specified' ±×·¸´Ù°í ÇÏ´Â ÅØ½ºÆ®°¡ Æ÷ÇԵ˴ϴÙ. Ç×7.8.2. ¡¸¿¹¿Ü¸¦ ó¸®ÇÏ´Â ¹æ¹ýÀº? ¡¹ ±×¸®°í ÃëÇÒ ¼ö ÀÖ°í ÀÖ´Â ¸Þ¼ÒµåÀÇ ¾î´ÀÂÊÀ̵çÀ¸·Î ÀÌ ¿¹¿Ü¸¦ Æ÷ÂøÇØ, °øÅëÀÇ ¿¡·¯ ÆäÀÌÁö ȤÀº Ȩ ÆäÀÌÁö¿¡ ¸®µð·ºÆ® ÇÕ´Ï´Ù.

  • Zend_Controller_Action::__call() (Àº)´Â, Á¸ÀçÇÏÁö ¾Ê´Â ¾×¼ÇÀ» ¸Þ¼Òµå¿¡ µð½ºÆÌÄ¡ ÇÒ ¼ö ¾ø¾ú´ø °æ¿ì¿¡ ¿¹¿Ü¸¦ ½½·Î¿ì ÇÕ´Ï´Ù. ÀÌ·¯ÇÑ °æ¿ì´Â, ¾î¶°ÇÑ µðÆúÆ® ¾×¼ÇÀ» ÄÜÆ®·Ñ·¯·Î »ç¿ëÇÏ°í ½ÍÀº °ÍÀÌÁö¿ä.±×·¯±â À§Çؼ­´Â ´ÙÀ½°ú °°ÀÌ ÇÕ´Ï´Ù.

    • Zend_Controller_Action ÀÇ ¼­ºê Ŭ·¡½º¸¦ ÀÛ¼ºÇØ, __call() ¸Þ¼Òµå¸¦ ¿À¹ö¶óÀ̵å(override) ÇÕ´Ï´Ù. ¿¹¸¦ µé¾î ´ÙÀ½°ú °°ÀÌ µË´Ï´Ù.

      
      <?php
      class My_Controller_Action extends Zend_Controller_Action
      {
          public function 
      __call($method$args)
          {
              if (
      'Action' == substr($method, -6)) {
                  
      $controller $this->getRequest()->getControllerName();
                  
      $url '/' $controller '/index';
                  return 
      $this->_redirect($url);
              }

              throw new 
      Exception('Invalid method');
          }
      }

      À§ÀÇ ¿¹´Â, ¹ÌÁ¤µµ¸®ÀÇ ¾×¼Ç ¸Þ¼Òµå°¡ ÄÝ µÇ¾úÀ» °æ¿ì¿¡ ±×°ÍÀ» ¸ðµÎ ¹Þ¾Æ, ±× ÄÜÆ®·Ñ·¯ÀÇ µðÆúÆ® ¾×¼Ç¿¡ ¸®µð·ºÆ® ÇÕ´Ï´Ù.

    • Zend_Controller_Dispatcher ÀÇ ¼­ºê Ŭ·¡½º¸¦ ÀÛ¼ºÇØ, getAction() ¸Þ¼Òµå¸¦ ¿À¹ö¶óÀ̵å(override) Çϰí, ¾×¼ÇÀÌ Á¸ÀçÇÒÁö¸¦ Á¶»çÇÕ´Ï´Ù. ¿¹¸¦ µé¾î ´ÙÀ½°ú °°ÀÌ µË´Ï´Ù.

      
      <?php
      class My_Controller_Dispatcher extends Zend_Controller_Dispatcher
      {
          public function 
      getAction($request)
          {
              
      $action $request->getActionName();
              if (empty(
      $action)) {
                  
      $action $this->getDefaultAction();
                  
      $request->setActionName($action);
                  
      $action $this->formatActionName($action);
              } else {
                  
      $controller $this->getController();
                  
      $action     $this->formatActionName($action);
                  if (!
      method_exists($controller$action)) {
                      
      $action $this->getDefaultAction();
                      
      $request->setActionName($action);
                      
      $action $this->formatActionName($action);
                  }
              }

              return 
      $action;
          }
      }

      À§ÀÇ ÄÚµå´Â, ÁöÁ¤ÇÑ ¾×¼ÇÀÌ ±× ÄÜÆ®·Ñ·¯ Ŭ·¡½º¿¡ Á¸ÀçÇÒÁö¸¦ Á¶»çÇÕ´Ï´Ù. Á¸ÀçÇÏÁö ¾Ê´Â °æ¿ì´Â, µðÆúÆ®ÀÇ ¾×¼Ç¿¡ ¸®¼ÂÆ® ÇÕ´Ï´Ù.

      ÀÌ ¹æ½ÄÀÇ ÀÌÁ¡Àº, ÃÖÁ¾ÀûÀ¸·Î µð½ºÆÌÄ¡¸¦ Çϱâ Àü¿¡ Åõ°úÀûÀ¸·Î ¾×¼ÇÀ» º¯°æÀ¸·Î ³ª¹«¹¯´Â °ÍÀÔ´Ï´Ù.±×·¯³ª À̰ÍÀº, URL (À»)¸¦ À߸ø ÃÆÀ» ¶§¿¡ µµ ¿Ã¹Ù¸£°í µð½ºÆÌÄ¡ µÇ¾î ¹ö¸®´Â °ÍÀ̱⵵ ÇÕ´Ï´Ù. À̰ÍÀº, ½áÄ¡¿£Áø ÃÖÀûÈ­¸¦ À§Çؼ­´Â ³Ê¹« ÁÁÁö ¾Ê½À´Ï´Ù.

    • Zend_Controller_Action::preDispatch() ȤÀº Zend_Controller_Plugin_Abstract::preDispatch() (À»)¸¦ »ç¿ëÇϰí, ¹«È¿ÀÎ ¾×¼ÇÀ» ÆÇº°ÇÕ´Ï´Ù.

      Zend_Controller_Action ÀÇ ¼­ºê Ŭ·¡½º¸¦ ÀÛ¼ºÇØ preDispatch() (À»)¸¦ º¯°æÇÏ´Â °ÍÀ¸·Î, ½ÇÁ¦·Î ¾×¼ÇÀ» µð½ºÆÌÄ¡ Çϱâ Àü¿¡ ÄÜÆ®·Ñ·¯³»¿¡¼­ ´Ù¸¥ ¾×¼Ç¿¡ Àü¼Û Çϰųª ¸®µð·ºÆ® Çϰųª ÇÏ´Â °ÍÀÌ °¡´ÉÇØÁý´Ï´Ù. ÀÌ ÄÚµå´Â, ¹æ±ÝÀü ¼³¸íÇß´Ù __call() (À»)¸¦ ¿À¹ö¶óÀ̵å(override) ÇÏ´Â ÄÚµå¿Í ´àÀº °ÍÀÌ µË´Ï´Ù.

      ¶Ç ÇϳªÀÇ ¹æ¹ýÀ¸·Î¼­´Â, ÀÌ Á¤º¸¸¦ ±Û·Î¹ú Ç÷¯±× ÀÎÀ¸·Î Á¶»çÇÒ ¼öµµ ÀÖ½À´Ï´Ù. ÀÌ ¹æ½ÄÀÇ ÀÌÁ¡Àº, ¾×¼Ç ÄÜÆ®·Ñ·¯¿Í´Â µ¶¸³Çϰí ÀÖ´Ù°í ÇÒ °÷ÀÔ´Ï´Ù. ¾îÇø®ÄÉÀ̼dz»¿¡¼­ ´Ù¾çÇÑ ¾×¼Ç ÄÜÆ®·Ñ·¯¸¦ »ç¿ëÇϰí ÀÖ´Ù°í ÇսôÙ. ±×°ÍµéÀÌ ¸ðµÎ µ¿ÀÏÇÑ Å¬·¡½º¸¦ °è½ÂÇϰí ÀÖ´Ù°í´Â ÇÒ ¼ö ¾ø½À´Ï´Ù. ±×·¯ÇÑ °æ¿ì¿¡ ÀÌ ¹æ½ÄÀ» »ç¿ëÇϸé, ´Ù¾çÇÑ Å¬·¡½º¿¡ ´ëÇØ¼­ ÀϰüÇÑ Ã³¸®¸¦ ½Ç½ÃÇÒ ¼ö ÀÖ½À´Ï´Ù.

      ¿¹¸¦ µé¾î ´ÙÀ½°ú °°ÀÌ µË´Ï´Ù.

      
      <?php
      class My_Controller_PreDispatchPlugin extends Zend_Controller_Plugin_Abstract
      {
          public function 
      preDispatch(Zend_Controller_Request_Abstract $request)
          {
              
      $dispatcher Zend_Controller_Front::getInstance()->getDispatcher();
              
      $controller $dispatcher->getController($request);
              if (!
      $controller) {
                  
      $controller $dispatcher->getDefaultControllerName($request);
              }
              
      $action     $dispatcher->getAction($request);

              if (!
      method_exists($controller$action)) {
                  
      $defaultAction $dispatcher->getDefaultAction();
                  
      $controllerName $request->getControllerName();
                  
      $response Zend_Controller_Front::getInstance()->getResponse();
                  
      $response->setRedirect('/' $controllerName '/' $defaultAction);
                  
      $response->sendHeaders();
                  exit;
              }
          }
      }

      ÀÌ ¿¹¿¡¼­´Â, ¸®Äù½ºÆ® µÈ ¾×¼ÇÀÌ ±× ÄÜÆ®·Ñ·¯¿¡ Á¸ÀçÇÒÁö¸¦ Á¶»çÇÕ´Ï´Ù. Á¸ÀçÇÏÁö ¾Ê´Â °æ¿ì´Â, ±× ÄÜÆ®·Ñ·¯ÀÇ µðÆúÆ® ¾×¼Ç¿¡ ¸®µð·ºÆ® ÇÕ´Ï´Ù. ±×¸®°í °Å±â¼­ ½ºÅ©¸³Æ®ÀÇ ½ÇÇàÀ» Á¾·áÇÕ´Ï´Ù.