|
| 1 | +# Nuxt Laravel |
| 2 | + |
| 3 | +[](http://commitizen.github.io/cz-cli/) |
| 4 | + |
| 5 | +## Jest coverage |
| 6 | + |
| 7 | +| Statements | Branches | Functions | Lines | |
| 8 | +| --------------------------- | ----------------------- | ------------------------- | ----------------- | |
| 9 | +|  |  |  |  | |
| 10 | + |
| 11 | +This package allows to develop a nuxt SPA as frontend for a laravel backend. |
| 12 | +The implementation is based on [laravel-nuxt-js](https://github.com/skyrpex/laravel-nuxt-js) by [skyrpex](https://github.com/skyrpex). |
| 13 | +> **Hint:** Use his composer exension [laravel-nuxt](https://github.com/skyrpex/laravel-nuxt) for dotenv support |
| 14 | +
|
| 15 | +## Installation |
| 16 | + |
| 17 | +Install the package and its peer dependencies. |
| 18 | + |
| 19 | +```bash |
| 20 | +npm install --save nuxt @nuxtjs/axios @nuxtjs/proxy nuxt-laravel |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +The package provides a binary which extends the `@nuxt/cli`, therefore you can use it directly with the nuxt cli command. |
| 26 | + |
| 27 | +In you package json: |
| 28 | + |
| 29 | +```json |
| 30 | +{ |
| 31 | + "scripts": { |
| 32 | + "dev": "nuxt laravel", |
| 33 | + "start": "npm run dev", |
| 34 | + "build": "nuxt laravel build" |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +## Commands |
| 40 | + |
| 41 | +### Development |
| 42 | + |
| 43 | +```bash |
| 44 | +npx nuxt laravel |
| 45 | +``` |
| 46 | + |
| 47 | +or |
| 48 | + |
| 49 | +```bash |
| 50 | +npx nuxt laravel dev |
| 51 | +``` |
| 52 | + |
| 53 | +Starts both Nuxt and Laravel artisan servers in development mode (hot-code reloading, error reporting, etc). |
| 54 | + |
| 55 | +#### Additional `dev` options |
| 56 | + |
| 57 | +In addition to the default `nuxt dev` command, the following options are provided: |
| 58 | + |
| 59 | +| option | description | default | |
| 60 | +| ---------------- | ------------------------------- | --------------------- | |
| 61 | +| `--render-path` | URL path used to render the SPA | `'/__nuxt_laravel__'` | |
| 62 | +| `--laravel-path` | Path to laravel directory | `process.cwd()` | |
| 63 | + |
| 64 | +If laravel path is provided a relative path it is resolved relative to `process.cwd()`. |
| 65 | + |
| 66 | +#### Laravel integration in development |
| 67 | + |
| 68 | +Render path is provided as environment variable `NUXT_URL` to `php artisan serve`. |
| 69 | +Use it in your `routes/web.php` to redirect all web traffic to or just use [laravel-nuxt](https://github.com/skyrpex/laravel-nuxt). |
| 70 | + |
| 71 | +**Example `routes/web.php`:** |
| 72 | + |
| 73 | +without `nuxt-laravel` |
| 74 | + |
| 75 | +```php |
| 76 | +// ... |
| 77 | +// Add this route the last, so it doesn't interfere with your other routes. |
| 78 | +Route::get( |
| 79 | + '{uri}', |
| 80 | + function($request, $uri) { |
| 81 | + // If the request expects JSON, it means that |
| 82 | + // someone sent a request to an invalid route. |
| 83 | + if ($request->expectsJson()) { |
| 84 | + abort(404); |
| 85 | + } |
| 86 | + |
| 87 | + // Fetch and display the page from the render path on nuxt dev server |
| 88 | + return file_get_contents(env('NUXT_URL')); |
| 89 | + } |
| 90 | +)->where('uri', '.*'); |
| 91 | +``` |
| 92 | + |
| 93 | +with `nuxt-laravel` |
| 94 | + |
| 95 | +```php |
| 96 | +// ... |
| 97 | +// Add this route the last, so it doesn't interfere with your other routes. |
| 98 | +Route::get( |
| 99 | + '{uri}', |
| 100 | + '\\'.Pallares\LaravelNuxt\Controllers\NuxtController::class |
| 101 | +)->where('uri', '.*'); |
| 102 | +``` |
| 103 | + |
| 104 | +### Production |
| 105 | + |
| 106 | +```bash |
| 107 | +laravel-nuxt build |
| 108 | +``` |
| 109 | + |
| 110 | +Compiles the application for production deployment. |
| 111 | + |
| 112 | +#### Additional `build` options |
| 113 | + |
| 114 | +In addition to the default `nuxt build` command, the following options are provided: |
| 115 | + |
| 116 | +| option | description | default | |
| 117 | +| --------------- | ------------------------------------------- | -------------------------- | |
| 118 | +| `--no-delete` | Do not delete build files after generation | `false` | |
| 119 | +| `--file-path` | Location for the SPA index file | `'storage/app/index.html'` | |
| 120 | +| `--public-path` | The folder where laravel serves assets from | `'public'` | |
| 121 | + |
| 122 | +If file path or public path are relative they are resolved relative to `rootDir` from `nuxt.config`. |
| 123 | + |
| 124 | +> **Attention:** If either path does not exists it is created recursively |
| 125 | +
|
| 126 | +#### Laravel integration in production |
| 127 | + |
| 128 | +If not provided, the file path is first checked against `process.env.NUXT_URL` so you can also set it using the environment variable. |
| 129 | +The command loads `require('dotenv').config()` so you can provide `NUXT_URL` in a `.env` file. |
| 130 | + |
| 131 | +Or just use [laravel-nuxt](https://github.com/skyrpex/laravel-nuxt). |
0 commit comments