0

I have an API created using Laravel and can be accessed through the URL: http://www.project.com/api using the virtual host in which http://www.project.com/ points to the public folder of the Laravel project. I also have a separate Vue project created using Vue CLI which connects and request data to the Laravel API and can be accessed through http://localhost:8080/.

My question is, can I integrate the 2 separate projects into a single project or URL and if yes, how?

6
  • this is called using nginx as a proxy. Read about upstream services in NGINX - generally it means have a / location that points to your VueJS nodejs server, and /api which gets routed to your PHP project Commented Mar 25, 2019 at 5:41
  • Here's an article that uses docker (but can pretty easily be rewritten to just support vanilla PHP) medium.com/@francoisromain/… Commented Mar 25, 2019 at 5:43
  • Just put your vue-project in the public folder? Commented Mar 25, 2019 at 5:59
  • Ok, thanks everyone for your reply. I will try your suggestions as soon as I get back home. Commented Mar 25, 2019 at 6:08
  • Did you solve this?. I have the same requirement Commented Feb 28, 2020 at 12:02

1 Answer 1

1

Laravel come out of the box with Laravel Mix which you can use for compiling your Vue project and serve it through Laravel.

Here are steps to do it.

  1. First you copy whole Vue project to /resources/render (you can change render to anything you like).

  2. Go to your Laravel root folder and open up file named webpack.mix.js and change it to like this

    const mix = require('laravel-mix');
    
    // Assuming your Vue entry point is index.js
    mix.js('resources/render/index.js', 'public/js');
    
  3. Create a blade file as entry point in resources/views. In my case it's main.blade.php.

    <!doctype html>
    <html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Laravel</title>
    </head>
    <body>
      <div id="app">
        {{-- Content --}}
      </div>
    </body>
    <script type="application/javascript" src="{{ mix('js/app.js') }}"></script>
    </html>
    
  4. Go to your routes/web.php and put only this route in. It will handles all routes in your Vue app

    Route::get('{path}', function () {
      return view('index');
    })->where('path', '^(.+)?$');
    
  5. Run npm install and npm run watch to build and react to changes you make to your Vue app. (You might want to remove unwanted dependencies from package.json)

I have a similar project with this setup. You can see it here for better understanding

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.