<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Piotr Krzempek</title>
    <description>The latest articles on DEV Community by Piotr Krzempek (@johnylemon).</description>
    <link>https://dev.to/johnylemon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F539654%2F1d3fd2e1-1343-4750-b1fa-1176e959e3e2.jpeg</url>
      <title>DEV Community: Piotr Krzempek</title>
      <link>https://dev.to/johnylemon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/johnylemon"/>
    <language>en</language>
    <item>
      <title>PHP Apidocs generation tool</title>
      <dc:creator>Piotr Krzempek</dc:creator>
      <pubDate>Sun, 13 Dec 2020 15:55:05 +0000</pubDate>
      <link>https://dev.to/johnylemon/php-apidocs-generation-tool-5do</link>
      <guid>https://dev.to/johnylemon/php-apidocs-generation-tool-5do</guid>
      <description>&lt;p&gt;I don't like writing tons of lines of stupid annotations just to have hope that api documentation will be generated correctly without errors that says nothing. And I am not the only one.&lt;/p&gt;

&lt;p&gt;So I created package that solves this problem the way I like - by writing PHP code.&lt;/p&gt;

&lt;p&gt;Can be found here: &lt;a href="https://github.com/johnylemon/laravel-apidocs"&gt;Laravel Apidocs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This way you can:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;reuse, extend and modify existing api definitions&lt;/li&gt;
&lt;li&gt;create generic endpoint definitions and just modify them ad-hoc or by creating child classes&lt;/li&gt;
&lt;li&gt;define multiple examples&lt;/li&gt;
&lt;li&gt;define multiple sample responses&lt;/li&gt;
&lt;li&gt;define and reuse parameter definitions&lt;/li&gt;
&lt;li&gt;make your controllers readable again&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Add &lt;code&gt;johnylemon/laravel-apidocs&lt;/code&gt; repository
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require johnylemon/laravel-apidocs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Register &lt;code&gt;Johnylemon\Apidocs\Providers\ApidocsServiceProvider&lt;/code&gt; provider if not registered automagically .&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install package. This command will publish all required assets.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan apidocs:install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Enjoy!&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Generating route documentation
&lt;/h2&gt;

&lt;p&gt;Package ships with command for rapid route definition generation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan apidocs:endpoint SampleEndpoint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Brand new &lt;code&gt;SampleEndpoint&lt;/code&gt; class will be placed within &lt;code&gt;app\Apidocs\Endpoints&lt;/code&gt; directory.&lt;br&gt;
Target directory may be changed within your &lt;code&gt;apidocs&lt;/code&gt; config file.&lt;/p&gt;

&lt;p&gt;This class contains only one &lt;code&gt;describe&lt;/code&gt; method, where you have to use any of available methods that will describe your endpoint. Like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Apidocs\Endpoints&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Johnylemon\Apidocs\Endpoints\Endpoint&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Johnylemon\Apidocs\Facades\Param&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SampleEndpoint&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Endpoint&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'List users'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Returns paginated list of users'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we set title and description as endpoint definition.&lt;br&gt;
Every method returns endpoint instance so you can chain them.&lt;/p&gt;
&lt;h2&gt;
  
  
  Endpoint definition usage
&lt;/h2&gt;

&lt;p&gt;Okay, you created your first endpoint definition. Now it's time to use it as some real route definition.&lt;/p&gt;

&lt;p&gt;Lets assume you have following routes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'api/users'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;UsersController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'index'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to use &lt;code&gt;App\Apidocs\Endpoints\SampleEndpoint&lt;/code&gt; class as definition for first of them you should simply do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Apidocs\Endpoints\SampleEndpoint&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'api/users'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;UsersController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'index'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;apidocs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SampleEndpoint&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and... yes, thats it!&lt;/p&gt;

&lt;p&gt;The only thing you have to do now is to call &lt;code&gt;php artisan apidocs:generate&lt;/code&gt; command and visit &lt;code&gt;/apidocs&lt;/code&gt; route to see it in action!&lt;/p&gt;

&lt;p&gt;Look at &lt;a href="https://github.com/johnylemon/laravel-apidocs"&gt;github package page&lt;/a&gt; to learn more!&lt;/p&gt;

&lt;p&gt;Developed with ❤ by &lt;a href="https://github.com/johnylemon"&gt;johnylemon&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>api</category>
      <category>github</category>
    </item>
  </channel>
</rss>
