A skeleton for creating applications with CakePHP 5.x.
The framework source code can be found here: cakephp/cakephp.
- Download Composer or update
composer self-update. - Run
php composer.phar create-project --prefer-dist cakephp/app [app_name].
If Composer is installed globally, run
composer create-project --prefer-dist cakephp/appIn case you want to use a custom app dir name (e.g. /myapp/):
composer create-project --prefer-dist cakephp/app myappYou can now either use your machine's webserver to view the default home page, or start up the built-in webserver with:
bin/cake server -p 8765Then visit http://localhost:8765 to see the welcome page.
-
Rest APIs setup CakePHP: To set up CakePHP for creating these APIs, you'll need to follow these steps:
-
Database Configuration: Configure your database settings in
config/app.php. You'll find the database configuration array in this file. Set your database connection details there. -
Generate the User Model: You can use the CakePHP console to generate the User model. Run the following command in your terminal:
bin/cake bake model UsersThis command will generate the User model based on the table named users in your database.
- Create the UsersController: Create a new controller named
UsersControllerusing the CakePHP console:
bin/cake bake controller UsersThis will create a UsersController with basic CRUD actions.
-
Implement the API Endpoints: Use the code provided in my previous message to implement the API endpoints in the UsersController.
-
Setup Routes: Configure the routes for your API endpoints in
config/routes.php. You can use CakePHP's routing system to map URLs to controller actions.
For example:
// config/routes.php
use Cake\Routing\Route\DashedRoute;
Router::defaultRouteClass(DashedRoute::class);
Router::scope('/', function ($routes) {
$routes->setExtensions(['json']); // Enable JSON response
$routes->resources('Users', [
'map' => [
'signup' => [
'action' => 'signup',
'method' => 'POST'
],
'login' => [
'action' => 'login',
'method' => 'POST'
],
'logout' => [
'action' => 'logout',
'method' => 'POST'
],
'resetPassword' => [
'action' => 'resetPassword',
'method' => 'POST'
],
'refreshToken' => [
'action' => 'refreshToken',
'method' => 'POST'
]
]
]);
});- To test your signup API endpoint in Postman, follow these steps:
Set Request Details:
- Choose the HTTP method as
POST. - Enter the URL for your signup API endpoint. For example, if your API endpoint is
http://localhost:8765/users/signup, enter this URL in the request URL field.
Set Headers (Optional): If your API requires specific headers, such as Content-Type, Accept, or custom headers, add them to the request headers section. For example, you might need to set Content-Type: application/json if your API expects JSON data.
Set Request Body: Depending on how your signup API endpoint is designed, you'll need to provide data in the request body. This typically includes information such as username, email, password, etc. If your API expects JSON data, select the "raw" option in the request body and enter the JSON data accordingly. For example:
{
"username": "example",
"email": "example@example.com",
"password": "password123"
}Send Request: Once you've set up the request details and included the necessary data, click on the "Send" button to send the request to your API endpoint.