# Laravel + Vue.js + Vue Router Setup Guide Panduan lengkap untuk setup Laravel dengan Vue.js 3, Vue Router, dan Tailwind CSS menggunakan Vite. ## Prerequisites - PHP >= 8.1 - Composer - Node.js >= 16.x - NPM atau Yarn ## Step 1: Instalasi Laravel ```bash # Buat project Laravel baru composer create-project laravel/laravel project-name # Masuk ke directory project cd project-name # Setup environment cp .env.example .env php artisan key:generate ``` ## Step 2: Instalasi Dependencies Frontend ```bash # Install Vue.js dan dependencies npm install vue@latest vue-router@latest # Install Vite plugins npm install --save-dev @vitejs/plugin-vue # Install Tailwind CSS npm install --save-dev tailwindcss@latest @tailwindcss/vite # Install utilities npm install --save-dev axios concurrently ``` ### Package.json Final ```json { "$schema": "https://json.schemastore.org/package.json", "private": true, "type": "module", "scripts": { "build": "vite build", "dev": "vite" }, "devDependencies": { "@tailwindcss/vite": "^4.0.0", "@vitejs/plugin-vue": "^6.0.1", "axios": "^1.11.0", "concurrently": "^9.0.1", "laravel-vite-plugin": "^2.0.0", "tailwindcss": "^4.0.0", "vite": "^7.0.4" }, "dependencies": { "vue": "^3.5.19", "vue-router": "^4.5.1" } } ``` ## Step 3: Konfigurasi Vite Buat/update `vite.config.js`: ```javascript import { defineConfig } from "vite"; import laravel from "laravel-vite-plugin"; import tailwindcss from "@tailwindcss/vite"; import vue from '@vitejs/plugin-vue'; export default defineConfig({ plugins: [ laravel({ input: ["resources/css/app.css", "resources/js/app.js"], refresh: true, }), tailwindcss(), vue() ], resolve: { alias: { vue: 'vue/dist/vue.esm-bundler.js', }, }, }); ``` ## Step 4: Setup Tailwind CSS Buat `tailwind.config.js`: ```javascript /** @type {import('tailwindcss').Config} */ export default { content: [ "./resources/**/*.blade.php", "./resources/**/*.js", "./resources/**/*.vue", ], theme: { extend: {}, }, plugins: [], } ``` Update `resources/css/app.css`: ```css @import 'tailwindcss'; ``` ## Step 5: Struktur Folder Frontend Buat struktur folder berikut di `resources/js/`: ``` resources/js/ ├── components/ │ └── App.vue ├── pages/ │ ├── Home.vue │ ├── About.vue │ └── Contact.vue ├── router/ │ └── index.js └── app.js ``` ## Step 6: Setup Vue Router ### `resources/js/router/index.js` ```javascript import { createRouter, createWebHistory } from 'vue-router' import Home from '../pages/Home.vue' import About from '../pages/About.vue' import Contact from '../pages/Contact.vue' const routes = [ { path: '/', name: 'Home', component: Home }, { path: '/about', name: 'About', component: About }, { path: '/contact', name: 'Contact', component: Contact } ] const router = createRouter({ history: createWebHistory(), routes }) export default router ``` ### `resources/js/app.js` ```javascript import { createApp } from 'vue'; import router from './router'; import App from './components/App.vue'; const app = createApp(App); app.use(router); app.mount('#app'); ``` ## Step 7: Buat Vue Components ### `resources/js/components/App.vue` ```vue ``` ### `resources/js/pages/Home.vue` ```vue ``` ### `resources/js/pages/About.vue` ```vue ``` ### `resources/js/pages/Contact.vue` ```vue ``` ## Step 8: Update Laravel Views ### `resources/views/welcome.blade.php` ```html Laravel + Vue.js @vite(['resources/css/app.css', 'resources/js/app.js'])
``` ## Step 9: Setup Laravel Routes (SPA) ### `routes/web.php` ```php where('any', '.*'); ``` ## Step 10: Jalankan Development Server ```bash # Terminal 1 - Laravel server php artisan serve # Terminal 2 - Vite dev server npm run dev ``` Atau gunakan concurrently (jika sudah diinstall): ```bash # Install concurrently jika belum npm install --save-dev concurrently # Tambahkan script di package.json "scripts": { "dev": "vite", "build": "vite build", "serve": "concurrently \"php artisan serve\" \"vite\"" } # Jalankan keduanya sekaligus npm run serve ``` ## Struktur Project Final ``` project-name/ ├── app/ ├── resources/ │ ├── css/ │ │ └── app.css │ ├── js/ │ │ ├── components/ │ │ │ └── App.vue │ │ ├── pages/ │ │ │ ├── Home.vue │ │ │ ├── About.vue │ │ │ └── Contact.vue │ │ ├── router/ │ │ │ └── index.js │ │ └── app.js │ └── views/ │ └── welcome.blade.php ├── routes/ │ └── web.php ├── package.json ├── vite.config.js ├── tailwind.config.js └── composer.json ``` ## Testing Buka browser dan kunjungi: - `http://localhost:8000` - Home page - `http://localhost:8000/about` - About page - `http://localhost:8000/contact` - Contact page ## Troubleshooting ### Error: "Component provided template option but runtime compilation is not supported" - Pastikan `vue` alias sudah ditambahkan di `vite.config.js` ### Error: "Failed to parse source for import analysis" - Pastikan `@vitejs/plugin-vue` sudah terinstall dan ditambahkan di plugins ### CSS tidak loading - Pastikan `@vite` directive sudah benar di blade file - Check jika Tailwind config sudah benar ### Router tidak bekerja - Pastikan Laravel routes catch-all sudah ditambahkan - Check browser console untuk error JavaScript ## Next Steps - Tambahkan authentication - Implementasikan API endpoints - Setup state management (Pinia/Vuex) - Tambahkan testing (Vitest) - Deploy ke production Selamat! Anda telah berhasil setup Laravel + Vue.js + Vue Router! 🎉