{"id":28886,"date":"2024-04-06T00:56:58","date_gmt":"2024-04-05T23:56:58","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/guia-rapida-para-principiantes-crea-tu-api-rest-con-slim-framework\/"},"modified":"2024-06-03T18:40:57","modified_gmt":"2024-06-03T17:40:57","slug":"guia-rapida-para-principiantes-crea-tu-api-rest-con-slim-framework","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/","title":{"rendered":"Quick Guide for Beginners: Create your REST API with Slim Framework"},"content":{"rendered":"<p>Slim Framework is a great, simplified tool for developers looking to build REST APIs quickly and efficiently. If you are a beginner and want to enter the world of API development, Slim offers a friendly learning curve and a clear structure to get started. This tutorial will guide you step by step in creating a REST API with Slim Framework, covering everything from installation to implementation of CRUD (Create, Read, Update, Delete) functions.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#%C2%BFQue_es_Slim_Framework\" >What is Slim Framework?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Configuracion_Inicial\" >Initial setup<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Requisitos_Previos\" >Previous requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Instalacion_de_Slim_Framework\" >Installing Slim Framework<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Estructura_de_Directorios\" >Directory Structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Configuracion_del_Entorno\" >Environment Configuration<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Creando_la_API_REST\" >Creating the REST API<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Definicion_de_Rutas\" >Definition of Routes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Implementacion_de_Funcionalidades_CRUD\" >Implementation of CRUD Functionalities<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Crear_POST\" >Create (POST)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Leer_GET\" >Read (GET)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Actualizar_PUT_y_Eliminar_DELETE\" >Update (PUT) and Delete (DELETE)<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Pruebas_y_Debugging\" >Testing and Debugging<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/nelkodev.com\/en\/php\/quick-guide-for-beginners-create-your-rest-api-with-slim-framework\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_Slim_Framework\"><\/span>What is Slim Framework?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Slim is a micro framework for PHP that allows you to write web applications and APIs in a simple and direct way. It is perfect for small to medium-sized projects, and its structural simplicity helps developers understand and manage code effectively.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Configuracion_Inicial\"><\/span>Initial setup<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Requisitos_Previos\"><\/span>Previous requirements<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Before you begin, make sure you have PHP and Composer installed on your system. Composer is a dependency manager for PHP that will make it easy to install Slim and other necessary libraries.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Instalacion_de_Slim_Framework\"><\/span>Installing Slim Framework<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To get started, create a new directory for your project and navigate to it in your terminal. Run the following command to install Slim Framework along with a basic skeleton that will serve as a starting point:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">composer create-project slim\/slim-skeleton your-api<\/code><\/pre>\n<p>This command sets up a new project called <code>your-api<\/code> and install all the necessary dependencies.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Estructura_de_Directorios\"><\/span>Directory Structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Once installed, your project will have the following directory structure:<\/p>\n<ul>\n<li><strong><code>src\/<\/code><\/strong>: Contains the source code files.<\/li>\n<li><strong><code>vendor\/<\/code><\/strong>: Directory where Composer installs the libraries.<\/li>\n<li><strong><code>public\/<\/code><\/strong>: Public folder where the file is located <code>index.php<\/code> which acts as the entry point of the application.<\/li>\n<li><strong><code>logs\/<\/code><\/strong>: To store the application logs.<\/li>\n<li><strong><code>templates\/<\/code><\/strong>: Directory for templates, in case your API serves HTML.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Configuracion_del_Entorno\"><\/span>Environment Configuration<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Configure your development environment to point to the directory <code>public\/<\/code>. This will ensure that all requests are directed to the file <code>index.php<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Creando_la_API_REST\"><\/span>Creating the REST API<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Definicion_de_Rutas\"><\/span>Definition of Routes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In Slim, routes are defined in the file <code>src\/routes.php<\/code>. This is where we will specify our API endpoints. For example, for an API that manages &quot;users&quot;, you could have routes like this:<\/p>\n<pre><code class=\"&quot;language-php&quot;\">$app-&gt;get(&#039;\/users&#039;, AppControllerUserController::class . &#039;:getUsers&#039;); $app-&gt;get(&#039;\/users\/{id}&#039;, AppControllerUserController::class . &#039;:getUser&#039;); $app-&gt;post(&#039;\/users&#039;, AppControllerUserController::class . &#039;:addUser&#039;); $app-&gt;put(&#039;\/users\/{id}&#039;, AppControllerUserController::class . &#039;:updateUser&#039;); $app-&gt;delete(&#039;\/users\/{id}&#039;, AppControllerUserController::class . &#039;:deleteUser&#039;);<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Implementacion_de_Funcionalidades_CRUD\"><\/span>Implementation of CRUD Functionalities<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4><span class=\"ez-toc-section\" id=\"Crear_POST\"><\/span>Create (POST)<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>To add a new user, your method in <code>UserController<\/code> could look like this:<\/p>\n<pre><code class=\"&quot;language-php&quot;\">public function addUser(Request $request, Response $response, array $args): Response { $data = $request-&gt;getParsedBody(); \/\/ Logic to add user \/\/ We respond with the added user and code 201 return $response-&gt;withJson($data, 201); }<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Leer_GET\"><\/span>Read (GET)<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>To get all users or a specific user by ID:<\/p>\n<pre><code class=\"&quot;language-php&quot;\">public function getUsers(Request $request, Response $response, array $args): Response { \/\/ Logic to get users return $response-&gt;withJson(1TP4YourUsers); } public function getUser(Request $request, Response $response, array $args): Response { $id = $args[&#039;id&#039;]; \/\/ Logic to get a specific user return $hresponse-&gt;withJson(1TP4YourUsuario); }<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Actualizar_PUT_y_Eliminar_DELETE\"><\/span>Update (PUT) and Delete (DELETE)<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Similar to the previous ones, it implements the corresponding logic in the methods <code>updateUser<\/code> y <code>deleteUser<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Pruebas_y_Debugging\"><\/span>Testing and Debugging<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To test your API, you can use tools like Postman or cURL. Testing is essential to ensure that all endpoints are working correctly and handling errors appropriately.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Creating a REST API with Slim Framework can be a great way to learn about backend development in a practical and direct way. With its simple architecture and powerful routing system, Slim is ideal for small and medium-sized projects and for developers just starting out in the world of APIs. Visit <a href=\"https:\/\/nelkodev.com\/en\/\">NelkoDev<\/a> for more resources or if you need <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">contact me<\/a>, don&#039;t hesitate to do it.<\/p>","protected":false},"excerpt":{"rendered":"<p>Slim Framework is an excellent, streamlined tool for developers looking to build REST APIs quickly and efficiently. If you are a beginner looking to get into the world of API development, Slim offers a friendly learning curve and a clear structure to get started. This tutorial will guide you step-by-step through building a REST API.<\/p>","protected":false},"author":1,"featured_media":28887,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2206,420,1913],"tags":[251,667,1344,205,90,484,358,60,15,175,1008,1998,1325,1999],"class_list":["post-28886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-blog","category-pruebas-de-backend","tag-crea","tag-api","tag-backend","tag-blog","tag-con","tag-framework","tag-guia","tag-para","tag-php","tag-principiantes","tag-pruebas","tag-rapida","tag-rest","tag-slim"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=28886"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28886\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/28887"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=28886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=28886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=28886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}