Quickstarters
CRUD Samples
How to Build a Basic CRUD App with PHP?
34 min
introduction in this guide, you'll learn how to develop a complete php application that performs create, read, update, and delete (crud) operations by leveraging the parse php sdk with your back4app project, you'll be able to seamlessly manage data and integrate robust backend features into your php app this tutorial covers everything from initializing your project and designing your data model to building secure php scripts for handling crud operations this comprehensive walkthrough ensures that you create a production ready application with secure user authentication, efficient data management, and an intuitive admin interface key takeaways understand how to construct crud operations using php alongside a backend service acquire hands on experience in designing a scalable data model learn how to utilize an intuitive admin panel to manage your backend effortlessly get acquainted with deployment strategies including containerization for your php application prerequisites before you begin, ensure you have the following a back4app account with a new project set up visit getting started with back4app https //www back4app com/docs/get started/new parse app if you need assistance a php development environment ensure you have php 7 4 or later installed along with a suitable web server setup basic knowledge of php, html, and rest apis refer to the php manual https //www php net/manual/en/ for further information step 1 – project setup starting your back4app project log in to your back4app account click the “new app” button on your dashboard name your project basic crud app php and follow the instructions to create it create new project once created, your new project will be visible on your dashboard, serving as the backbone for your php crud application step 2 – crafting your data model designing your schema for this application, you'll define several classes directly in your backend the examples below illustrate the classes and fields you should set up using the back4app dashboard 1\ items class this class holds details for every item field type explanation id objectid auto generated unique identifier title string the name of the item description string a short overview of the item created at date time when the item was created updated at date time when the item was last modified 2\ users class this class manages user details and authentication field type explanation id objectid auto generated unique identifier username string a unique identifier for the user email string the user's email address password hash string securely stored hashed password created at date timestamp when the account was created updated at date timestamp for the latest account update you can manually create these classes in your back4app dashboard by adding new classes and specifying the appropriate fields create new class simply choose the desired data type, set the field name, and determine whether it’s required or has a default value create column step 3 – admin panel & crud operations in php overview of the admin interface the back4app admin app provides an easy to use, drag and drop interface for managing your backend data this tool lets you perform crud operations effortlessly without extra code activating the admin app navigate to the “more” menu in your back4app dashboard select “admin app” and then click “enable admin app ” configure your admin account by creating your first administrative user, which also sets up default roles enable admin app after activation, log into the admin app to manage your classes and execute crud operations admin app dashboard within this panel you can add new records insert new entries into classes like items view and modify records click on a record to see its details or update its fields delete records remove records that are no longer required this intuitive interface greatly simplifies the task of managing your backend data step 4 – connecting php with your backend now that your backend is configured, it's time to link your php application using the parse php sdk option a using the parse php sdk install the parse php sdk use composer to add the sdk to your project composer require parse/php sdk set up parse in your php application create a configuration file (e g , parseconfig php ) \<?php require 'vendor/autoload php'; use parse\parseclient; // initialize parse with your back4app credentials parseclient initialize('your application id', 'your rest api key', 'your master key'); parseclient setserverurl('https //parseapi back4app com', '/'); ?> 3\ retrieve items using php for example, create a script named `itemslist php` ```php \<?php require 'parseconfig php'; use parse\parsequery; $query = new parsequery("items"); try { $results = $query >find(); foreach ($results as $item) { echo "\<p>\<strong>" $item >get("title") "\</strong> " $item >get("description") "\</p>"; } } catch (exception $ex) { echo "error " $ex >getmessage(); } ?> this script retrieves all records from the items class and displays them option b using rest or graphql apis if the parse sdk is not an option, you can perform crud actions via rest for instance, to fetch items using php and curl \<?php $url = 'https //parseapi back4app com/classes/items'; $headers = \[ "x parse application id your application id", "x parse rest api key your rest api key" ]; $ch = curl init($url); curl setopt($ch, curlopt httpheader, $headers); curl setopt($ch, curlopt returntransfer, true); $response = curl exec($ch); curl close($ch); $data = json decode($response, true); foreach ($data\['results'] as $item) { echo "\<p>\<strong>" $item\['title'] "\</strong> " $item\['description'] "\</p>"; } ?> integrate these code snippets within your php application as needed step 5 – safeguarding your backend implementing access control lists (acls) enhance your application's security by setting acls on objects the following example demonstrates how to create a private item \<?php require 'parseconfig php'; use parse\parseobject; use parse\parseacl; function createprivateitem($data, $owner) { $item = new parseobject("items"); $item >set("title", $data\['title']); $item >set("description", $data\['description']); $acl = new parseacl($owner); $acl >setpublicreadaccess(false); $acl >setpublicwriteaccess(false); $item >setacl($acl); try { $item >save(); echo "private item created successfully "; } catch (exception $e) { echo "error " $e >getmessage(); } } // example usage // createprivateitem(\['title' => 'sample item', 'description' => 'a description'], $currentuser); ?> configuring class level permissions (clps) within the back4app dashboard, adjust the clps for each class so that only authorized users have access to sensitive data step 6 – user authentication setting up user registration and login back4app uses parse’s built in user class for authentication the example below shows how to handle user signup in php \<?php require 'parseconfig php'; use parse\parseuser; if ($ server\['request method'] === 'post') { $user = new parseuser(); $user >set("username", $ post\['username']); $user >set("password", $ post\['password']); $user >set("email", $ post\['email']); try { $user >signup(); echo "registration successful!"; } catch (exception $ex) { echo "error " $ex >getmessage(); } } ?> \<! html form for signup > \<form method="post" action="signup php"> \<input type="text" name="username" placeholder="username" required> \<input type="password" name="password" placeholder="password" required> \<input type="email" name="email" placeholder="email" required> \<button type="submit">sign up\</button> \</form> you can create similar scripts for login and session management additional features such as social authentication and email verification can also be set up via the back4app dashboard step 7 – deploying your php application back4app’s web deployment service supports php applications as well follow these steps to deploy your project 7 1 organize your application ensure your project directory is structured like this basic crud app php/ ├── vendor/ ├── public/ \| └── index php ├── src/ \| ├── parseconfig php \| ├── itemslist php \| └── auth php ├── composer json └── readme md 7 2 upload your code to github initialize a git repository in your project folder git init stage your files git add commit your changes git commit m "initial commit for php crud application" create a github repository for instance, name it basic crud app php push your code to github git remote add origin https //github com/your username/basic crud app php git git push u origin main 7 3 integrate github with web deployment access web deployment log in to your back4app dashboard, go to your project, and click on web deployment connect your github account follow the prompts to link your github repository select your repository and branch choose basic crud app php and the main branch where your php code resides 7 4 configure deployment settings set up your deployment configuration with build command if necessary, specify a command to prepare your application output directory point to the folder (e g , public/ ) that serves as your document root environment variables add any required api keys or configuration settings 7 5 dockerizing your php application (optional) if you prefer containerization, add a dockerfile to your repository \# use an official php image with apache from php 8 1 apache \# copy your application files to the container copy /var/www/html/ \# expose port 80 for web traffic expose 80 cmd \["apache2 foreground"] configure back4app to deploy your containerized application if desired 7 6 deploy your application click the deploy button initiate deployment once all configurations are complete monitor the deployment process back4app will pull your repository, run any build commands, and deploy your php application access your application url after deployment, a url will be provided where your app is live 7 7 verify your deployment visit the provided url open the url in your browser to confirm your php application is running test crud functionality ensure that all create, read, update, and delete operations perform as expected troubleshoot if needed use browser developer tools or check back4app logs for any issues step 8 – final thoughts and future directions congratulations! you have successfully built a php based crud application using back4app throughout this tutorial, you set up a project named basic crud app php designed clear classes for items and users utilized the admin app for seamless data management connected your php scripts to the backend using the parse php sdk secured your data with acls and implemented user authentication next steps enhance the user interface expand your php application with features like detailed item views, search functionality, or real time updates integrate advanced features consider adding cloud functions, third party api integrations, or role based access control deepen your knowledge explore the back4app documentation https //www back4app com/docs and other resources to further enhance your development skills by following this tutorial, you are now equipped to develop robust php applications that efficiently manage data happy coding!