3.0.3 • Published 6 years ago

nuxt-laravel v3.0.3

Weekly downloads
31
License
MIT
Repository
github
Last release
6 years ago

Nuxt Laravel

Commitizen friendly

Jest coverage

StatementsBranchesFunctionsLines
StatementsBranchesFunctionsLines

This package allows to develop a nuxt SPA as frontend for a laravel backend.
The implementation is based on laravel-nuxt-js by skyrpex.

Hint: Use his composer exension laravel-nuxt for dotenv support

Installation

Install the package and its peer dependencies.

npm install --save nuxt @nuxtjs/axios @nuxtjs/proxy nuxt-laravel

Usage

The package provides a binary which extends the @nuxt/cli, therefore you can use it directly with the nuxt cli command.

In you package json:

{
  "scripts": {
    "dev": "nuxt laravel",
    "start": "npm run dev",
    "build": "nuxt laravel build"
  }
}

Commands

Development

npx nuxt laravel

or

npx nuxt laravel dev

Starts both Nuxt and Laravel artisan servers in development mode (hot-code reloading, error reporting, etc).

Additional dev options

In addition to the default nuxt dev command, the following options are provided:

optiondescriptiondefault
--render-pathURL path used to render the SPA'/__nuxt_laravel__'
--laravel-pathPath to laravel directoryprocess.cwd()

If laravel path is relative it is resolved relative to process.cwd().

Laravel integration in development

Render path and app url are provided as environment variables NUXT_URL and APP_URL respectively to php artisan serve.
Use it in your routes/web.php to redirect all web traffic to or just use laravel-nuxt.

!!! Attention !!!: You have to use PHPs getenv() instead of laravels env() as it ignores putenv vars.

config/app.php:

    // ...
    /*
    |--------------------------------------------------------------------------
    | Application URL
    |--------------------------------------------------------------------------
    |
    | This URL is used by the console to properly generate URLs when using
    | the Artisan command line tool. You should set this to the root of
    | your application so that it is used when running Artisan tasks.
    |
    */

    'url' => getenv('APP_URL') ?: 'http://localhost',

    // ...

Example without nuxt-laravel:

routes/web.php:

// ...
// Add this route last, so it doesn't interfere with your other routes.
Route::get(
    '{uri}',
    function($request, $uri) {
      // ...
      // If the request expects JSON, it means that
      // someone sent a request to an invalid route.
      if ($request->expectsJson()) {
          abort(404);
      }

      // Fetch and display the page from the render path on nuxt dev server
      return file_get_contents(getenv('NUXT_URL') ?: storage_path('app/index.html'));
    }
)->where('uri', '.*');

Example with nuxt-laravel:

routes/web.php:

// ...
// Add this route last, so it doesn't interfere with your other routes.
Route::get(
    '{uri}',
    '\\'.Pallares\LaravelNuxt\Controllers\NuxtController::class
)->where('uri', '.*');

config/nuxt.php:

return [
    /**
     * In production, the SPA page will be located in the filesystem.
     *
     * In development, the SPA page will be on an external server. This
     * page will be passed as an environment variable (NUXT_URL).
     */
    'page' => getenv('NUXT_URL') ?: storage_path('app/index.html')
];

Production

laravel-nuxt build

Compiles the application for production deployment.

Additional build options

In addition to the default nuxt build command, the following options are provided:

optiondescriptiondefault
--no-deleteDo not delete build files after generationboolean option has no value
--file-pathLocation for the SPA index file'storage/app/index.html'
--public-pathThe folder where laravel serves assets from'public'

If file path or public path are relative they are resolved relative to rootDir from nuxt.config.

!!! Attention !!!: If either path does not exists it is created recursively

Laravel integration in production

If not provided, the file path is first checked against process.env.NUXT_URL, so you can also set it using the environment variable.
The command loads require('dotenv').config() so you can provide NUXT_URL through an .env file.

Or just use laravel-nuxt.

4.2.9

5 years ago

4.2.8

5 years ago

4.2.7

5 years ago

4.2.6

5 years ago

4.2.5

5 years ago

4.2.4

5 years ago

4.2.3

5 years ago

4.2.2

5 years ago

4.2.1

5 years ago

3.1.5

5 years ago

4.2.0-beta.18

5 years ago

4.2.0-beta.17

5 years ago

4.2.0-beta.16

5 years ago

4.2.0-beta.15

5 years ago

4.2.0-beta.14

5 years ago

4.2.0-beta.13

6 years ago

4.2.0-beta.11

6 years ago

4.2.0-beta.10

6 years ago

4.2.0-beta.9

6 years ago

4.2.0-beta.8

6 years ago

4.2.0-beta.7

6 years ago

4.2.0-beta.6

6 years ago

4.2.0-beta.5

6 years ago

4.2.0-beta.4

6 years ago

4.2.0-beta.3

6 years ago

4.2.0-beta.2

6 years ago

4.2.0-beta.1

6 years ago

4.2.0-beta.0

6 years ago

4.1.9

6 years ago

4.1.8

6 years ago

4.1.7

6 years ago

4.1.6

6 years ago

4.1.5

6 years ago

4.1.4

6 years ago

4.1.3

6 years ago

4.1.2

6 years ago

4.1.1

6 years ago

3.1.4

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.1.3

6 years ago

3.1.2

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.2.0

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago