Skip to content

Latest commit

 

History

History
120 lines (99 loc) · 9.32 KB

File metadata and controls

120 lines (99 loc) · 9.32 KB

Magento 2 REST API Full Course

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Introduction to REST API in Magento 2

Magento 2 uses REST API to connect with third-party systems, allowing custom endpoints for extensions. Focus on creating CRUD operations (Create, Read, Update, Delete) using webapi.xml and PHP classes/interfaces. Tools include Magento Open Source 2.4.6, Postman for requests, and Cloudways for hosting. Builds on a popup extension from a prior course.

Setting Up Interfaces and Services

For API support, create interfaces and their implementations in the extension. PopupRepositoryInterface handles CRUD methods, implemented by a service class. If missing, add getList for listing entities. Use di.xml for preferences mapping interfaces to classes.

Configuring webapi.xml

webapi.xml maps endpoints to services, specifying HTTP methods, resources for ACL, and interfaces. Draw from examples like the CMS module. For the popup entity, define routes for operations, using anonymous access or specific permissions.

  • Key Takeaway: Routes like /V1/magemastery/popup/:popupId tie to methods like getById, with ACL resources for access control.
<route url="/V1/magemastery/popup/:popupId" method="GET">
    <service class="MageMastery\Popup\Api\PopupRepositoryInterface" method="getById"/>
    <resources><resource ref="anonymous"/></resources>
</route>

Implementing GET (Read) Operation

For reading a popup by ID, use GET on /V1/magemastery/popup/:popupId. The getById method returns the popup data or throws an exception if not found. Test in developer mode for error traces.

Implementing PUT (Update) and POST (Create) Operations

Use PUT on /V1/magemastery/popup/:popupId for updates and POST on /V1/magemastery/popup for creates, both mapping to the save method. JSON body includes fields like name, content, and is_active (as integer). Returns the entity ID.

Implementing DELETE Operation

DELETE on /V1/magemastery/popup/:popupId uses the deleteById method, returning true on success. Verify by attempting a GET afterward, which should fail if deleted.

Implementing GET List (Search) Operation

GET on /V1/magemastery/popup/search uses getList with SearchCriteriaInterface. Implement in repository using collection factory, processor, and search results factory. Returns array of popups with total count.

  • Key Takeaway: Add SearchResultsInterface with getItems/setItems, and process collections for filters.
public function getList(SearchCriteriaInterface $searchCriteria): SearchResultsInterface
{
    $collection = $this->popupCollectionFactory->create();
    $this->collectionProcessor->process($searchCriteria, $collection);
    $searchResults = $this->searchResultsFactory->create();
    $searchResults->setSearchCriteria($searchCriteria);
    $searchResults->setItems($collection->getItems());
    $searchResults->setTotalCount($collection->getSize());
    return $searchResults;
}

Testing with Postman

Use Postman to send GET, POST, PUT, DELETE requests to local or remote endpoints. JSON bodies for create/update; check database changes via phpMyAdmin. Clear cache if needed.

Deploying to Cloud Hosting

Deploy extensions to Cloudways via SSH: Add Git repos to composer.json, require packages, enable modules, run setup:upgrade. Test APIs on the production URL.

Conclusion and Recommendations

Wrap up by testing on remote servers and sharing. Check related videos on Cloudways setup and prior popup course for context.


About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: