Connectionmanager connect returning fatal error instead of redirect in CakePHP 3

982 Views Asked by At

I'm working in CakePHP 3.4

In my component, I'm checking if database connection could be installed or not. If database connection couldn't be established, redirect user to Installation plugin to setup database configuration.

This is how I'm checking in my component.

<?php
 namespace Installer\Controller\Component;

 use Cake\Controller\Component;
 use Cake\Controller\ComponentRegistry;
 use Cake\Datasource\ConnectionManager;
 use Cake\Core\Configure;
 use Cake\Filesystem\File;

 /**
  * Install component
  */
 class InstallComponent extends Component
 {
     public function installationCheck()
     {
         // connection to the database
         try {
             $db = ConnectionManager::get('default');

             if(!$db->connect()) {      // line 23
                return $this->redirect(['plugin' => 'Installer', 'controller' => 'Install', 'action' => 'index']);
             }
         } catch (Exception $e) {

         }


        return true;
     }
 }

But when database connection failed, Instead of redirecting user, it gives fatal error as

Connection to database could not be established: SQLSTATE[HY000] [1045] Access denied for user 'user'@'host' (using password: YES)

Stack Trace

⟩ Cake\Database\Connection->connect ROOT/plugins/Installer/src/Controller/Component/InstallComponent.php, line 23
⟩ Installer\Controller\Component\InstallComponent->installationCheck APP/Controller/AppController.php, line 65
⟩ App\Controller\AppController->beforeRender CORE/src/Event/EventManager.php, line 414
⟩ Cake\Event\EventManager->_callListener CORE/src/Event/EventManager.php, line 391
⟩ Cake\Event\EventManager->dispatch CORE/src/Event/EventDispatcherTrait.php, line 78
⟩ Cake\Controller\Controller->dispatchEvent CORE/src/Controller/Controller.php, line 610
⟩ Cake\Controller\Controller->render APP/Controller/PagesController.php, line 61
⟩ App\Controller\PagesController->display CORE/src/Controller/Controller.php, line 440
⟩ Cake\Controller\Controller->invokeAction CORE/src/Http/ActionDispatcher.php, line 119
⟩ Cake\Http\ActionDispatcher->_invoke CORE/src/Http/ActionDispatcher.php, line 93
⟩ Cake\Http\ActionDispatcher->dispatch CORE/src/Http/BaseApplication.php, line 78
⟩ Cake\Http\BaseApplication->__invoke CORE/src/Http/Runner.php, line 65
⟩ Cake\Http\Runner->__invoke CORE/src/Routing/Middleware/RoutingMiddleware.php, line 59
⟩ Cake\Routing\Middleware\RoutingMiddleware->__invoke CORE/src/Http/Runner.php, line 65
⟩ Cake\Http\Runner->__invoke CORE/src/Routing/Middleware/AssetMiddleware.php, line 88
⟩ Cake\Routing\Middleware\AssetMiddleware->__invoke CORE/src/Http/Runner.php, line 65
⟩ Cake\Http\Runner->__invoke CORE/src/Error/Middleware/ErrorHandlerMiddleware.php, line 92
⟩ Cake\Error\Middleware\ErrorHandlerMiddleware->__invoke CORE/src/Http/Runner.php, line 65
⟩ Cake\Http\Runner->__invoke ROOT/vendor/cakephp/debug_kit/src/Middleware/DebugKitMiddleware.php, line 52
⟩ DebugKit\Middleware\DebugKitMiddleware->__invoke CORE/src/Http/Runner.php, line 65
⟩ Cake\Http\Runner->__invoke CORE/src/Http/Runner.php, line 51
⟩ Cake\Http\Runner->run CORE/src/Http/Server.php, line 80
⟩ Cake\Http\Server->run ROOT/webroot/index.php, line 37
1

There are 1 best solutions below

3
On BEST ANSWER

If you check the definition for the ConnectionManager::get() method you'll see that it either returns a connection object or throws a MissingDatasourceConfigException. So you might need to wrap your code in a try catch block:-

public function installationCheck()
{
    try {
        $db = ConnectionManager::get('default');

        // connection to the database
        if (!$db->connect()) {
           return $this->redirect(['plugin' => 'Installer', 'controller' => 'Install', 'action' => 'index']);
        }
    } catch (\Exception $e) {
    }

    return true;
}