2014-07-02 47 views
2

當我輸入網址,如:如何在Controller中不存在Phalcon時備份到IndexController?

http://localhost/asdfasdfasdcxccarf

爾康是給我這個消息:

PhalconException: AsdfasdfasdcxccarfController handler class cannot be loaded

這是合乎邏輯的,因爲該控制器不存在。

但是,我該如何讓Phalcon將每個沒有控制器的錯誤URL重定向到我的默認控制器IndexController?

回答

8

您可以將以下調度程序服務添加到依賴項注入容器。它會檢查內部爾康錯誤(如錯誤的控制器爲例)並轉發用戶到指定的控制器和動作:

$di->set('dispatcher', function() { 

    $eventsManager = new \Phalcon\Events\Manager(); 

    $eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) { 

     //Handle 404 exceptions 
     if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) { 
      $dispatcher->forward(array(
       'controller' => 'index', 
       'action' => 'show404' 
      )); 
      return false; 
     } 

     //Handle other exceptions 
     $dispatcher->forward(array(
      'controller' => 'index', 
      'action' => 'show503' 
     )); 

     return false; 
    }); 

    $dispatcher = new \Phalcon\Mvc\Dispatcher(); 

    //Bind the EventsManager to the dispatcher 
    $dispatcher->setEventsManager($eventsManager); 

    return $dispatcher; 

}, true); 
2

你可以在你二叔的初始化設置缺省路由

$di->set('router', function() { 
    $router = new \Phalcon\Mvc\Router(); 

    //your routes here 

    $router->setDefaults(array(
     'controller' => 'index', 
     'action' => 'index' 
    )); 

    return $router; 
}); 

所以, 當調度員未發現行動時,他將使用CurentController => indexAction 否則,當調度員未找到控制員時,他將使用IndexController => indexAction

相關問題