Symfony 4, PHP 7 used to develop a online shop RESTfull API
- Symfony 4
- PHP 7.2
- Appache/Nginx
- Mysql 5.6
git clone https://github.com/shadman/shop-online.git
cd shop-online
composer install
Create a database with credentials like shop_online
and user and password
Update .env file parameter DATABASE_URL
for created database connection string:
DATABASE_URL=mysql://:@127.0.0.1:3306/shop_online
php bin/console make:migration
php bin/console doctrine:migrations:migrate
php bin/console doctrine:fixtures:load --purge-with-truncate
METHOD: POST
URL: http://localhost/shop-online/public/api/register
REQUEST:
{
"email":"demo@app.com",
"plainPassword":"123456",
"fullName":"Demo User"
}
Login to get access token and to access other API endpoints
METHOD: POST
URL: http://localhost/shop-online/public/api/login
REQUEST:
{
"email":"demo@app.com",
"assword":"123456"
}
RESPONSE:
{
"apikey": "6e847cd99e8a1e915001-1558878749"
}
This token will be required for every API call in headers
Fetch Products list, set 'X-AUTH-TOKEN' in request headers
METHOD: GET
URL: http://localhost/shop-online/public/api/products
Header Name: X-AUTH-TOKEN
Header Value: dbe7a82479ae4aea7f44-1558876797
METHOD: GET
URL: http://localhost/shop-online/public/api/products?page=2&limit=3
METHHOD: GET
URL: http://localhost/shop-online/public/api/customers/orders/summary
RESPONSE:
{
"productsIdsArr" : {
"0" : {"productId" : 1,"quantity" : 2},
"1" : {"productId" : 2,"quantity" : 2},
"2" : {"productId" : 4,"quantity" : 2}
}
}
Save customer order details
METHOD: POST
URL: http://localhost/shop-online/public/api/customers/orders
RESPONSE:
{
"fullName":"John Smith",
"email":"john@app.com",
"contactNumber":"+92787887878",
"postalCode":"46000",
"shippingAddress":"House#xyz, Street abc",
"city":"City name",
"country":"Pakistan",
"customerNotes":"I need urgent service...",
"productsIdsArr":{
"0": {"productId":1,"quantity":1},
"1": {"productId":2,"quantity":1},
"2": {"productId":4,"quantity":1}
}
}
METHOD: GET
URL: http://localhost/shop-online/public/api/customers/orders/1
METHOD: GET
URL: http://localhost/shop-online/public/api/customers/orders
NOTE: apitoken will be mandatory to access endpoints.
METHOD: POST
URL: http://localhost/shop-online/public/api/products
RESPONSE:
{
"title": "My final product 1",
"slug": "my-final-product-1",
"price": 220,
"isDiscount": "No",
"discountType": null,
"discount": 0,
"isProductBundle": "No",
"sku": null,
"status": "Active",
"imageType": "Link",
"image": "https://image.shutterstock.com/image-vector/grunge-red-sample-word-round-260nw-1242668641.jpg",
"description": null
}
METHOD: PUT
URL: http://localhost/shop-online/public/api/products/19
RESPONSE:
{
"title": "My final product 11",
"slug": "my-final-product-1",
"price": 222,
"isDiscount": "No",
"discountType": null,
"discount": 0,
"isProductBundle": "No",
"sku": null,
"status": "Active",
"imageType": "Link",
"image": "https://image.shutterstock.com/image-vector/grunge-red-sample-word-round-260nw-1242668641.jpg",
"description": null
}
METHOD: GET
http://localhost/shop-online/public/api/products/19
METHOD: DELETE
URL: http://localhost/shop-online/public/api/products/19
List all products which dont have bundles so by that we can display to select when creating new bundle
METHOD: GET
URL: http://localhost/shop-online/public/api/products/notbundles
METHOD: POST
URL: http://localhost/shop-online/public/api/products/bundles
RESPONSE:
{
"title": "My final product bundle",
"slug": "my-final-product-bundle",
"price": 220,
"isDiscount": "No",
"discountType": null,
"discount": 0,
"isProductBundle": "Yes",
"sku": null,
"status": "Active",
"imageType": "Link",
"image": "https://image.shutterstock.com/image-vector/grunge-red-sample-word-round-260nw-1242668641.jpg",
"description": null,
"productsArr":{"0":23,"1":12}
}
METHOD: PUT
URL: http://localhost/shop-online/public/api/products/bundles/25
RESPONSE:
{
"title": "My final product bundle",
"slug": "my-final-product-bundle",
"price": 220,
"isDiscount": "No",
"discountType": null,
"discount": 0,
"isProductBundle": "Yes",
"sku": null,
"status": "Active",
"imageType": "Link",
"image": "https://image.shutterstock.com/image-vector/grunge-red-sample-word-round-260nw-1242668641.jpg",
"description": "Awesome description",
"productsArr":{"0":3,"1":4}
}
METHOD: GET
URL: http://localhost/shop-online/public/api/products/bundles/25
METHOD: DELETE
URL: http://localhost/shop-online/public/api/products/bundles/25
Run following command on project root:
./bin/phpunit
- If you get error
Failed asserting that 403 matches expected 200.
updateHTTP_X-AUTH-TOKEN
with apikey in alltests/Controllers
, you may get that apikey fromuser
tableapi_token
Cheers !