<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Cristian Pallarés  on Medium]]></title>
        <description><![CDATA[Stories by Cristian Pallarés  on Medium]]></description>
        <link>https://medium.com/@skyrpex?source=rss-9a94da5a146b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*IYEFo9KFtvhtM1Xo.</url>
            <title>Stories by Cristian Pallarés  on Medium</title>
            <link>https://medium.com/@skyrpex?source=rss-9a94da5a146b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 01:58:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@skyrpex/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Create a SPA with Laravel and Nuxt]]></title>
            <link>https://medium.com/@skyrpex/create-a-spa-with-laravel-and-nuxt-a4034fcb1955?source=rss-9a94da5a146b------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4034fcb1955</guid>
            <category><![CDATA[single-page-applications]]></category>
            <category><![CDATA[nuxtjs]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[api-development]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Cristian Pallarés ]]></dc:creator>
            <pubDate>Fri, 14 Sep 2018 07:03:40 GMT</pubDate>
            <atom:updated>2018-09-14T07:03:40.052Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*2KcuzXA9zPNxntHc4pqaZg.png" /></figure><p>In this tutorial we’ll use <a href="https://laravel.com/">Laravel</a> as an API and <a href="https://nuxtjs.org/">Nuxt</a> as a Single Page Application. They can work together, but it’s not an easy task at first. If you already tried, getting HMR to seamlessly work is a pain! For that reason I created <a href="https://github.com/skyrpex/laravel-nuxt">laravel-nuxt</a> and <a href="https://github.com/skyrpex/laravel-nuxt-js">laravel-nuxt-js</a>.</p><p>There are more reasons to use these packages, such as when using <a href="https://laravel.com/docs/5.5/passport">Laravel Passport</a> and the CreateFreshApiToken. The middleware will create an api_token cookie on web routes that use the get http verb, and that&#39;s a problem if you&#39;re not serving your SPA from within Laravel.</p><h3>Getting started</h3><h3>Install Laravel</h3><p>Let’s start with fresh new Laravel installation:</p><pre>composer create-project laravel/laravel spa</pre><p>Go inside the spa directory with your terminal.</p><h3>Install laravel-nuxt (for PHP)</h3><pre><em># cd spa</em><br>composer require pallares/laravel-nuxt</pre><p>This package will be autodiscovered. If you’re using old versions of Laravel, just add the service provider in config/app.php file:</p><pre>&lt;?php<br>return [<br>    // ...<br>    &#39;providers&#39; =&gt; [<br>        // ...<br>        Pallares\LaravelNuxt\LaravelNuxtServiceProvider::class,<br>    ],<br>];</pre><p>Add a fallback route that will render the SPA page in routes/web.php file. Be sure to remove the default route that comes with the framework:</p><pre>&lt;?php<br>// Route::get(&#39;/&#39;, function () {<br>//     return view(&#39;welcome&#39;);<br>// });<br><br>Route::get(<br>    &#39;{uri}&#39;,<br>    &#39;\\&#39;.Pallares\LaravelNuxt\Controllers\NuxtController::class<br>)-&gt;where(&#39;uri&#39;, &#39;.*&#39;);</pre><p>Now, your backend is ready to serve the compiled assets that Nuxt will generate for you. Every route that would return a 404 now will serve our SPA page.</p><h3>Install laravel-nuxt (for JS)</h3><p>It’s time to install the JS package. Replace your package.json file with this:</p><pre>{<br>    &quot;private&quot;: true,<br>    &quot;scripts&quot;: {<br>        &quot;start&quot;: &quot;laravel-nuxt dev&quot;,<br>        &quot;build&quot;: &quot;laravel-nuxt build&quot;<br>    },<br>    &quot;dependencies&quot;: {<br>        &quot;laravel-nuxt&quot;: &quot;^1.0.0&quot;<br>    }<br>}</pre><p>Install the dependencies:</p><pre>npm install</pre><p>The laravel-nuxt package will install Nuxt for you, along with <a href="https://github.com/vuejs/vue">Vue</a>, <a href="https://github.com/vuejs/vue-router">vue-router</a>, <a href="https://github.com/nuxt-community/axios-module">@nuxtjs/axios</a>, etc. Let&#39;s create the nuxt.config.js file:</p><pre>const laravelNuxt = require(&quot;laravel-nuxt&quot;);<br><br>module.exports = laravelNuxt({<br>  // Options such as mode, srcDir and generate.dir are already handled for you.<br>  modules: [],<br>  plugins: [],<br>});</pre><p>From now on, Nuxt will look for the source files in the resources/nuxt directory. Create a hello world route in resources/nuxt/pages/index.vue:</p><pre>&lt;template&gt;<br>  &lt;h1&gt;Hello {{ name }}!&lt;/h1&gt;<br>&lt;/template&gt;<br><br>&lt;script&gt;<br>export default {<br>  data: () =&gt; {<br>    return { name: &#39;world&#39; };<br>  },<br>};<br>&lt;/script&gt;</pre><p>Finally, run:</p><pre>npm start</pre><p>Go to <a href="http://localhost:8000/">http://localhost:8000</a>. You should see this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*utgnwcBBBUkP7Ct9.jpg" /></figure><p>That’s it! Laravel artisan’s server and Nuxt’s dev server are up and working together transparently. Try editing your home page now, it’s very enjoyable to see the live reload in action. Under the hood, Nuxt’s dev server is proxying every call to the Laravel’s server, including the SPA rendering. Since @nuxtjs/axios module is included (and proxied, too), you can make API calls normally.</p><h3>Calling the API from the SPA</h3><p>The SPA will surely need to call our API, so let’s add a route to routes/api.php to retrieve the user information:</p><pre>&lt;?php<br>Route::get(&#39;me&#39;, function () {<br>    // Let&#39;s return fake information.<br>    return [<br>        &#39;name&#39; =&gt; &#39;John Doe&#39;,<br>    ];<br>});</pre><p>Now, edit resources/nuxt/pages/index.vue:</p><pre>&lt;template&gt;<br>  &lt;h1&gt;Hello {{ user.name }}!&lt;/h1&gt;<br>&lt;/template&gt;<br><br>&lt;script&gt;<br>export default {<br>    // https://github.com/nuxt-community/axios-module<br>    async asyncData({ app }) {<br>        const user = await app.$axios.$get(&#39;api/me&#39;);<br>        return { user };<br>    },<br>};<br>&lt;/script&gt;</pre><p>Voila! Your page should now look like this!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yPs4ga8nD_wY_n8F.jpg" /></figure><blockquote><em>In order to keep the tutorial simple, we aren’t using any kind of authentication here. Integrating Passport should be almost trivial here.</em></blockquote><h3>Deployment</h3><p>If you want to deploy your application, just run npm run build. The compiled assets will be placed in the public/_nuxt directory. You can preview your final app with the well known artisan command php artisan serve.</p><blockquote><em>You may want to add the </em><em>.nuxt and </em><em>public/_nuxt directories to your </em><em>.gitignore.</em></blockquote><h3>Final thoughts</h3><p>This is my first tutorial. I hope everything is clear and concise! Please, don’t hesitate to ask questions here or create issues in the laravel-nuxt repositories. Thanks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4034fcb1955" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>