@awes-io/nuxt-laravel

AwesCode UI module for connection with Laravel backend
7,075 9
Install
npm install @awes-io/nuxt-laravel
Latest Version:0.8.1
License:MIT
Last Updated:May 7, 2023
Links: GitHub  ·  npm
Maintainer: awescode

AwesCode UI Nuxt Laravel Module

Seamless integration module for Nuxt.js applications with Laravel backends.

Overview

@awes-io/nuxt-laravel provides complete integration between Nuxt.js and Laravel, handling API proxying, build configuration, and deployment workflows.

Key Features:

  • API Proxy: Automatic proxying of /api and /broadcasting routes to Laravel
  • Build Integration: Builds Nuxt files directly into Laravel's public directory
  • Environment Configuration: Easy setup with environment variables
  • Version Plugin: Display app version and build date
  • Development Workflow: Hot reload with Laravel backend proxy
  • Production Build: Optimized SPA build for Laravel serving

Quick Start

Installation

yarn add @awes-io/nuxt-laravel

Requirements:

  • Laravel 8+ with @awes-io/laravel-nuxt composer package
  • Nuxt.js 2.x (SPA mode recommended)

Project Structure

project-root/
├── app/                    # Laravel application
├── resources/
│   └── nuxt/               # Nuxt source code
│       ├── assets/
│       ├── components/
│       ├── pages/
│       └── ...
├── storage/app/nuxt/       # Nuxt build output
├── public/                 # Laravel public (serves SPA)
├── nuxt.config.js          # Nuxt config (root)
├── package.json            # NPM dependencies (root)
└── composer.json           # Composer dependencies

Basic Setup

1. Configure Nuxt (nuxt.config.js):

export default {
    mode: 'spa',
    srcDir: 'resources/nuxt',

    modules: [
        '@awes-io/nuxt-laravel'  // Include first
    ]
}

2. Add Scripts (package.json):

{
    "scripts": {
        "dev": "LARAVEL_URL=http://localhost:8000 nuxt",
        "build": "LARAVEL_URL=http://localhost:8000 nuxt build",
        "generate": "LARAVEL_URL=http://localhost:8000 nuxt generate"
    }
}

3. Configure Environment (.env):

LARAVEL_URL=http://localhost:8000
FRONTEND_URL=http://localhost:3000

Development Workflow

# Start Laravel backend
php artisan serve

# Start Nuxt dev server
yarn dev

Navigate to http://localhost:3000 - API requests are automatically proxied to Laravel.

Production Build

yarn generate

Builds Nuxt to storage/app/nuxt/ and copies to public/_nuxt/. Laravel serves the SPA.

Documentation

Comprehensive documentation is available in the /docs folder:

Module Options

export default {
    modules: [
        ['@awes-io/nuxt-laravel', {
            generateDir: 'storage/app/nuxt',  // Custom build directory
            versionPlugin: {
                name: 'My Application',
                version: 'v1.0.0',
                date: '2024-01-15'
            }
        }]
    ]
}

What the Module Does

1. Includes Axios Module

Automatically adds and configures @nuxtjs/axios.

2. Configures API Proxy

Development:

  • /api/* → proxied to http://localhost:8000/api/*
  • /broadcasting/* → proxied to http://localhost:8000/broadcasting/*

Production:

  • All requests → proxied to Laravel backend

3. Configures Build Output

  • Sets generate.dir to storage/app/nuxt/
  • Copies built files to public/_nuxt/ on generate
  • Creates public/index.html as SPA entry point

4. Adds Version Plugin

Logs app version on page load:

console.log('[MyApp] Build Version: v1.0.0 (2024-01-15T10:30:00.000Z)')

Environment Variables

Variable Required Description
LARAVEL_URL Yes Laravel backend URL
NON_PROXY_URL No Direct API URL (skip proxy)
FRONTEND_URL No Frontend URL (for CORS)
APP_NAME No Application name
APP_VERSION No App version
APP_VERSION_DATE No Build date

Common Usage Patterns

Making API Calls

// Automatically proxied to Laravel
const response = await this.$axios.get('/api/users')

Fetch Data on Page Load

<template>
    <div>
        <ul>
            <li v-for="item in items" :key="item.id">
                {{ item.name }}
            </li>
        </ul>
    </div>
</template>

<script>
export default {
    async asyncData({ $axios }) {
        const response = await $axios.get('/api/items')
        return { items: response.data.data }
    }
}
</script>

Form Submission

<script>
export default {
    methods: {
        async submit() {
            try {
                await this.$axios.post('/api/items', this.form)
                this.$notify({ message: 'Created!', type: 'success' })
            } catch (error) {
                this.$notify({ message: 'Failed', type: 'error' })
            }
        }
    }
}
</script>

Development

Git Commit Convention

Ensure to write proper commit message according to Git Commit convention

Troubleshooting

API Requests Not Proxied

Check LARAVEL_URL environment variable:

echo $LARAVEL_URL
# Should output: http://localhost:8000

CORS Errors

Configure Laravel CORS in config/cors.php:

'paths' => ['api/*', 'broadcasting/*'],
'allowed_origins' => [env('FRONTEND_URL', 'http://localhost:3000')],
'supports_credentials' => true,

External Resources

Related Packages

laravel-nuxt

Build a SPA with Laravel and Nuxt

4,089 146
nuxt-outfit

Outfit - Connection Laravel and Nuxt

2,727 0
nuxt-laravel-precognition

Nuxt module to work with Laravel Precognition and $fetch

2,009 10
@scayle/storefront-nuxt

Nuxt integration for the SCAYLE Commerce Engine and Storefront API

152,903

Version History

Version Released License
0.8.1 MIT
0.8.0 MIT
0.7.2 MIT
0.7.1 MIT
0.7.0 MIT