Build a Single Page Application using CakePHP and InertiaJS
The Inertia Plugin allows a CakePHP application to integrate Vue 3 components in the front end, without the need to write a specific API for data transfer.
This is done by adding a Middleware and view classes that facilitate the conversion of objects and data in JSON almost automatically, as well as the direct load in the components.
The plugin is thought of as a base to extend and use your app’s specific controllers and views from.
Just because it works out of the box doesn't mean it is intended to be used exactly as is, but this will provide you a good kick start.
See the repo here: https://github.com/CakeDC/cakephp-inertia
Requirements
-
CakePHP 4.5
-
PHP >= 8.1
-
NodeJS 18.9 (only for build Vue Components, not required on running site)
Step 1: Create a basic CakePHP install
For this example I will use a basic installation using Docker and Composer.
First you must create project from cakephp/app
|
Then write an docker-compose.yml file as:
|
Launch the container and go to http://localhost:9099/
|
Step 2: Add CakePHP Inertia plugin
Install plugin via command line:
|
Once installed enable it in src/Application.php, adding at the bottom of bootstrap function:
|
or by command line:
|
Step 3: Create Vue App and install it
To create Vue App type in command line:
|
This command create in the resources directory the files that use our App, also create in root directory the files:
-
webpack.mix.js
-
package.json
Then in root directory install with NPM:
|
Step 4: Create simple SPA (Single Page Application)
Create a single page called dashboard that show values sets in a controller action
We need to first add InertiaResponseTrait
|
Create a new function that would look like this:
|
in config/routes.php uncomment lines to catch all routes:
|
and comment line:
|
Then create file resources/js/Components/Pages/Dashboard.vue that would look like this:
|
On root directory execute:
|
IMPORTANT: Whenever you modify the .vue templates, you must run this script.
Go to http://localhost:9099/pages/dashboard to see that Dashboard Vue Component prints values assignments on Dashboard CakePHP function.
Step 5: Bake CRUD system
For this example, we use sql file on config/sql/example/postgresql.pgsql
That creates a database with the relations
Once the database has been created, bake models and controllers as normal using:
|
and bake templates using vue_template instead of template as:
|
Again run:
|
You can the results from this example by going to http://localhost:9099/pages/index
In the following recording you can see how to add, edit and delete a record without reloading the page at any time.
Step 6: Using prefix and adding a navigation menu
Add route to prefix Admin on config/routes.php
|
To generate controllers and template with a prefix use --prefix option of bake command as:
|
You can add a horizontal menu to navigate through controllers
Edit resources/Components/Layout.vue and put inside header tag links as:
|
Again run:
|
You can see the results from this example by going to http://localhost:9099/admin/pages/index
In the following recording you can see how to add, edit and delete a record without reloading the page at any time and navigate through pages, tags and categories.
Hopefully this example will make your experience easier! Let us know: [email protected].