Use CakePHP Without Database, Using Dummy Driver

CakePHP is a great framework (before you argue, this post isn’t about CakePHP vs Laravel), allowing you to kick-start development rather quickly with minimal effort. However, there’s one small thing that irks me with its setup. CakePHP requires a database connection, whether you have a use for it or not.

Now, there might be a way to use CakePHP without database but I haven’t really found a clean way in terms of config to simply disable the use of database, not up-till version 3.6.5 at-least. And hence the second best option was the simply force CakePHP into believing that it has connectivity with the database, when in-fact, it doesn’t. Here’s how you could do so,

  1. Create a file called Dummy.php under /src/Database/Driver (create directory structure if it doesn’t exist).

  2. Use the following code in Dummy.php,

    <?php
    namespace Cake\Database\Driver;
    use PDO;
    
    class Dummy
    {
        public function enabled()
        {
            return true;
        }
        function connect() {
            $this->connected = true;
            return $this->connected;
        }
        function disconnect() {
            $this->connected = false;
            return !$this->connected;
        }
        function isConnected() {
            return true;
        }
    }
    
  3. In app.php located under /config, as part of Datasources array, set the default driver to Dummy as follows,

        'Datasources' => [
            'default' => [
                'className' => 'Cake\Database\Connection',
                'driver' => 'Dummy'
        ],
    

That’s pretty much it. This will allow you to use CakePHP without requiring any connectivity to database! The idea is pretty straight forward here, create a shallow custom database driver mimicking an always-connected database connection.

Here’s more on managing database configuration in CakePHP.

Categories: PHP