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
Category: CakePHP
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
Reading and writing geospatial in CakePHP 5.1+
CakePHP 5.1.0 introduced support for geospatial types, but for now it’s limited. Here’s how you can read and write these values easily.
“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
Optional parameter in named routes
Let’s say you want to make a named CakePHP route which may or may not have an argument. For example, you want to return WHOIS information for an IP address: /whois/1.1.1.1 returns WHOIS information for 1.1.1.1 which is specifically passed in the URL; /whois will return WHOIS information for the visitor’s IP address. When doing… Continue reading Optional parameter in named routes
Changing request IP address for debugging in CakePHP
If you’re working on a feature that acts differently based on the request IP address, you would be wanting to test it continuously. For that, you may have to have your application think you’re accessing it from a specific/different IP address. The easiest way would be to just store the IP in a variable and… Continue reading Changing request IP address for debugging in CakePHP
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