我在CakePHP 3.4中工作
在我的组件中,我正在检查是否可以安装数据库连接。如果无法建立数据库连接,请将用户重定向到Installation
插件以设置数据库配置。
这就是我检查我的组件的方式。
<?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;
}
}
但是当数据库连接失败时,不会重定向用户,而是会出现致命错误
无法建立与数据库的连接:SQLSTATE [HY000] [1045]拒绝用户'用户'@'主持人'的访问权限(使用密码:是)
堆栈跟踪
⟩ 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
答案 0 :(得分:1)
如果检查ConnectionManager::get()
方法的定义,您将看到它返回连接对象或抛出MissingDatasourceConfigException
。因此,您可能需要将代码包装在try catch块中: -
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;
}