<?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: Emilien Kopp</title>
    <description>The latest articles on DEV Community by Emilien Kopp (@emilien_kopp_88dd3031992b).</description>
    <link>https://dev.to/emilien_kopp_88dd3031992b</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1718926%2F3663a722-7a47-48b4-9c6c-183ddb013954.jpg</url>
      <title>DEV Community: Emilien Kopp</title>
      <link>https://dev.to/emilien_kopp_88dd3031992b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emilien_kopp_88dd3031992b"/>
    <language>en</language>
    <item>
      <title>Inertia and API responses living together in harmony</title>
      <dc:creator>Emilien Kopp</dc:creator>
      <pubDate>Thu, 23 Jul 2026 03:40:11 +0000</pubDate>
      <link>https://dev.to/emilien_kopp_88dd3031992b/inertia-and-api-responses-living-together-in-harmony-3mdl</link>
      <guid>https://dev.to/emilien_kopp_88dd3031992b/inertia-and-api-responses-living-together-in-harmony-3mdl</guid>
      <description>&lt;h2&gt;
  
  
  I love InertiaJS to the point where it's becoming a personality trait
&lt;/h2&gt;

&lt;p&gt;I tend to want to use it for everything, but adding Inertia to an existing Laravel API gets awkward fast. Same thing happens in the other direction: you start with a full Inertia frontend and then realize you want to expose some of that data as a public API too.&lt;/p&gt;

&lt;p&gt;The naive solutions are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sprinkle &lt;code&gt;if ($request-&amp;gt;wantsJson())&lt;/code&gt; into your controllers&lt;/li&gt;
&lt;li&gt;Maintain two separate routes that return the exact same data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Neither feels right. So I made &lt;strong&gt;&lt;a href="https://github.com/EmilienKopp/inertia-split" rel="noopener noreferrer"&gt;inertia-split&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Starting fresh with Inertia: serve both from the same controller
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProjectController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&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;HasHybridResponses&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;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&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;respond&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;component&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Projects/Index'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'projects'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Project&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Inertia request → renders the Svelte/Vue/React component&lt;/span&gt;
          &lt;span class="c1"&gt;// API request     → returns JSON&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;The controller doesn't check anything. Inertia requests get an Inertia response, API clients get JSON.&lt;/p&gt;

&lt;h2&gt;
  
  
  Existing API? Don't touch it
&lt;/h2&gt;

&lt;p&gt;If you just want to make an existing API method Inertia-aware, one annotation is enough:&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="na"&gt;#[InertiaComponent('Users/Show')]&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;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;User&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'user'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$user&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;The method body stays exactly as it was.&lt;/p&gt;

&lt;p&gt;Inertia requests get the component rendered with your data as props.&lt;/p&gt;

&lt;p&gt;Everything else gets the same JSON as before.&lt;br&gt;
Methods without the annotation are completely unaffected.&lt;/p&gt;
&lt;h3&gt;
  
  
  Wait, how does this even work?
&lt;/h3&gt;

&lt;p&gt;The package can out Inertia's ResponseFactory for its own in the service provider (opt-in):&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="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;singleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ResponseFactory&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="nc"&gt;HybridResponseFactory&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="c1"&gt;// checks if it's an Inertia request and returns appropriate response&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good old OOP. Thank you polymorphism.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap-up
&lt;/h2&gt;

&lt;p&gt;Whatever the direction of your problem, making Inertia and API endpoints use the same controller is a big win.&lt;br&gt;
You're still responsible for writing routes and wiring middlewares, but this should save a lot of time and effort.&lt;/p&gt;




&lt;p&gt;Still in beta, use accordingly!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/EmilienKopp/inertia-split" rel="noopener noreferrer"&gt;https://github.com/EmilienKopp/inertia-split&lt;/a&gt;&lt;/p&gt;

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