Custom file extensions (e.g. .env): routing and rendering serialized responses

It is fairly common to add json handler to a CakePHP route and have it automatically respond with formatted JSON. Consider this example. In your route: $routes->prefix(‘Api’, function (RouteBuilder $routes) { $routes->setExtensions([‘json’]); $routes->fallbacks(DashedRoute::class); }); In your controller: public function initialize(): void { parent::initialize(); $this->addViewClasses([\Cake\View\JsonView::class]); $this->viewBuilder()->setOption(‘serialize’, true); } So now when you request actions in that… Continue reading Custom file extensions (e.g. .env): routing and rendering serialized responses

Deploying a simple CakePHP app to Render.com using Docker

Render doesn’t have a ā€œnative PHP runtimeā€ like it does for Node/Python, but it does have Docker. So we can use a Docker image that will set up the PHP runtime for us. To make it happen, we will have to add these two files to our application: Dockerfile that imports and uses a prebuilt… Continue reading Deploying a simple CakePHP app to Render.com using Docker

Connection to Sqlite not established, unable to open database file

The problem You created a new CakePHP application and you try to visit its homepage. You get a database error. Missing Database Connection Cake\Database\Exception\MissingConnectionException Connection to Sqlite could not be established: SQLSTATE[HY000] [14] unable to open database file The (potential) reason Check your composer.json and see if your application uses cakephp/debug_kit: “require-dev”: { … “cakephp/debug_kit”:… Continue reading Connection to Sqlite not established, unable to open database file

“Authentication is required to continue” when loading Authentication component

If you use cakedc/users or cakephp/authentication and want to programmatically log in a newly created user, you will have to use setIdentity() on Authentication component, and for that you will have to load it into your controller. But if you do it wrong, you will get this error: Authentication is required to continue Authentication\Authenticator\UnauthenticatedException To… Continue reading “Authentication is required to continue” when loading Authentication component

CRUD View: _getIndexTitleField() must be of type string, array returned

Setup You’re using CakePHP with CRUD plugin installed. You baked some models from the database. Problem You visit the index page for a given model and you get the following error: CrudView\Listener\ViewListener::_getIndexTitleField(): Return value must be of type string, array returned Solution Make sure the model has a display field. You can always try using… Continue reading CRUD View: _getIndexTitleField() must be of type string, array returned

Complex CASE-THEN-WHEN expressions as columns in select queries

(Below has been tested on CakePHP 5, but should work on 3+.) Say you’re working on a query that may have to do some currency conversion on the go if user’s currency is different from the currency in your database. To make it worse, your site may use a third currency as a base currency… Continue reading Complex CASE-THEN-WHEN expressions as columns in select queries

Getting started with Paginator: a minimal example

The built-in pagination changed in more recent CakePHP versions (4 and 5), it is now a view helper and no longer a controller component. MVP In your Controller where you want to use pagination, load the helper: public function initialize(): void { parent::initialize(); $this->viewBuilder()->addHelper(‘Paginator’); } Paginate your query results before passing them to the view… Continue reading Getting started with Paginator: a minimal example

Published
Categorised as CakePHP