Many laravel/vue tutorials use ajax calls to get the data. It seems that the SPA is completely isolated from Laravel. I.e. Laravel is just a data API and the vue app could also simply be hosted on a third party external server (e.g. AWS S3). Is this the recommended way, or should I rather use Laravel for Routing and have separate views that implement individual components and already included data instead of using a SPA?
For an SPA, I would recommend just going with the standard setup, which is Laravel on the webserver and Vue in the browser. To do this, install Laravel and Vue. AJAX communications from the browser to the server are accomplished with the Axios library which comes with Vue. Here is how to install Laravel and Vue router:
composer require laravel/ui
php artisan ui vue
npm install && npm run dev
npm install vue-router
npm run watch
From within a Vue component, using Axios to communicate with the server looks like the following. Also, in the following, the endpoint is defined in the Laravel > Routes > web.php:
methods: {
fetchMessages() {
let endpoint = `/channels/${this.activeChannel}/messages`;
axios.get(endpoint).then(resp => {
this.messages = resp.data.messages;
});
},
A Vue router is declared in the main js file. For instance, in app.js.
Here is what a Vue router looks like, and additional url paths would be added under "routes":
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
base: '/',
mode: 'history',
history: true,
routes: [
{
path: '/',
name: 'home',
component: PostComponent
},
],
});
Related
I have a little problem with vue.js recently and wondering if anyone could help please?
I have a new install of laravel 7.3.3 and vue.js 2.6.12 can not get the vue devtools extension to show me my components, variables etc.
This is what the devtools look like :
Here is the repo : https://github.com/yex777/pa-hub
Thanks for any help!
I had a similar issue with my repo. I don't actually register components globally, I instead make components and just register them on the page they are needed. My issue with this was caused because I was binding Vue to the page twice. Once in app.js and then again in my custom JS file for my component. As such, I think the dev tools was picking up the wrong Vue instance.
I resolved my issue by removing the references to vue in my app.js file and then only binding elements using my new JS file. So my app.js file only contains
require('./bootstrap');
and then I have a separate file which I include on the relevant page which looks a little like this:
import Vue from 'vue';
import Axios from 'axios';
import StudentManagement from './StudentManagement.vue';
window.axios = Axios;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
new Vue({ // eslint-disable-line no-undef, no-new
el: '#student-management',
render: h => h(StudentManagement),
});
I suspect you may have a similar issue where the Vue instance is being setup twice.
The easiest way to check is in the console, it will output a message saying that Vue is in development mode:
As you can see, it's got a 2 next to the message, saying the message has been output twice. If this is the case, you just need to make sure you only include or set up your Vue instance once and the dev tools should pickup your components.
I've built an SPA with Laravel, Vue js, Vuex and Laravel Passport for authentication.
I have a vuex store file that contains the apiURL and serverPath that I use throughout my application via services, where I make my axios requests.
store.js:
import Vue from 'vue';
import Vuex from 'vuex';
import * as auth from './services/auth_service.js';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
apiURL: 'http://highrjobsadminlte.test/api',
serverPath: 'http://highrjobsadminlte.test',
profile: {},
},
mutations: {
authenticate(state, payload) {
state.isLoggedIn = auth.isLoggedIn();
if (state.isLoggedIn) {
state.profile = payload;
} else {
state.profile = {};
}
}
},
actions: {
authenticate(context, payload) {
context.commit('authenticate', payload)
}
}
});
As you can see in the apiURL and serverPath variables, I'm developing on my localhost using this domain: http://highrjobsadminlte.test
After deploying my application to GoDaddy, I changed these variables to my actual domain: https://highrjobs.com
Now I'm getting this error:
app.js:26402 Mixed Content: The page at 'https://highrjobs.com/candidate/search' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://highrjobsadminlte.test/api/candidate/search'. This request has been blocked; the content must be served over HTTPS.
When I click on the app.js file, I can see that it still has the store.js code with the local domain in the apiURL and serverPath variables, But they are changed in the files on the Server 1000%.
I thought it's a caching issue so I ran these commands:
php artisan route:cache
php artisan cache:clear
php artisan config:cache
and it doesn't fix the issue. How can I fix this?
Mixed content: This request has been blocked
this error mostly get because
you are using https://highrjobs.com domain which is https protocol and u are using some url inside your application which is not https
apiURL: 'http://highrjobsadminlte.test/api',
serverPath: 'http://highrjobsadminlte.test',
like this u need to move in to https
in this error
app.js:26402 Mixed Content: The page at 'https://highrjobs.com/candidate/search' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://highrjobsadminlte.test/api/candidate/search'. This request has been blocked; the content must be served over HTTPS.
you can clearly see server try to serve https://highrjobs.com/candidate/search this secure url this unsecured api http://highrjobsadminlte.test/api/candidate/search
so fix this u need to use https to your api url as well
hope i explained well
and your code should be
apiURL: 'http://highrjobsadminlte.test/api' to apiURL: 'https://highrjobs.com/api',
Thank you
Sorry, I forgot to run npm run production before deploying. So my app.js file still had the old URL's.
I have crate two separate project, one in vuejs (with vue create project_name) and the other in laravel (with laravel new project_name). Now i want to send data from vuejs project to the laravel project. How can i do that?
You can use a library like axios or fetch in your vuejs app to make http requests to your laravel app
(Axios docs: https://github.com/axios/axios)
(Fetch docs: https://developer.mozilla.org/es/docs/Web/API/Fetch_API/Utilizando_Fetch)
I like axios
For example if you want to make a POST to your api you can do
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
or a GET
axios.get('/user?ID=12345')
axios.get and axios.post are async functions, so you have to use the inside an async function whit await or use callbacks
I'm working on a Laravel project where all frontend is managed by vuejs.
So, I have all backend routes defined on:
routes/api.php
And the file: routes/web.php it has this only route:
Route::view('{all}', 'app')
->where('all', '^((?!api).)*')
->name('vue');
Giving all the responsability to vue.js.
So inside resources/js (vue side) I have the frontend routes defined, for example:
{
path: '/',
name: 'home',
component: home,
meta: {
auth: true,
}
},
{
path: '/invoices/show/:id',
name: 'invoices.show',
component: InvoicesShow,
meta: {
auth: true,
}
},
Now the problem:
I started trying to create some notifications.
So on the notification I have to put the link where the customer has to go, but this link is to the frontend... So in Laravel I cannot write the vue.js route. Well, that's the question, is it possible to write the named route of vue.js?
I'd like to define this link on a notification for example:
->action('View Invoice', route('invoices.show', $invoiceId));
But the named route "invoices.show" it doesn't exist on Laravel routes. It's defined on vuejs router, in: resources/js/router/routes.js
So it won't work.
Is possible to get the vuejs router by name?
For now, I find impossible to do this.
So I simply write the full url "hardcoded".
->action('View Invoice', route('vue', ['all' => "invoices/show/{$invoiceId}"]));
This resolves my problem, but I cannot use the vue.js named routes.
One of the dirty workarounds would be to leave the VueJs route as it is and to create a new identical "helper" route for Laravel in your routes/web.php file.
So your web.php would look like this:
Route::view('/invoices/show/{id}', 'app')->name('invoices.show');
//Here you can add additional "helpers" routes as needed
Route::view('{all}', 'app')
->where('all', '^((?!api).)*')
->name('vue');
In this case, VueJs would still work fine and you would be able to use route() helper function in your project but a disadvantage is that you would need to maintain two routes if there is any changes.
I am developing a laravel(5.4) application in which i am using vue js(2.0) for SPA, my problem is i want to remove #(hash) from URL.
please suggest me any solution?
This is my HTML
<ul class="category-list">
<li><router-link to="/testlink.html">Tets Link</router-link></li>
<li><router-link to="/demotestlink.html">Demo Test LInk</router-link></li>
</ul>
This is my vue js code
export default new VueRouter({
[
{
path: '/testlink.html',
component:require('./components/demo/Testlink')
}
],
mode: 'history',
});
and i have made a Testlink.vue file inside components/demo folder in Assets
Note: FYI, I am using vue.js(2.0) in Laravel(5.4) Application
From Vue-Router documentation, I found this.
The default mode for vue-router is hash mode - it uses the URL hash to simulate a full URL so that the page won't be reloaded when the URL changes.
To get rid of the hash, we can use the router's history mode, which leverages the history.pushState API to achieve URL navigation without a page reload:
const router = new VueRouter({
mode: 'history',
routes: [...]
})
I tried the above and it worked fine for me. For more information read here